新学习struts,一个小实例,仅供参考。步骤也许不完善,希望共同进步
1>创建web项目StrutsTest;
2>在web.xml中配置struts;
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- struts 的核心控制器filterdispatcher被设计成过滤器,通过<filter/>引入;
<url-pattern>/*</url-pattern>表示所有的客户端请求都经过filterdispatcher过滤,然后把请求给struts2进行处理。
-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/login/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3>创建struts.xml;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- 放在网页顶部的doctype声明是让浏览器进入正确呈现模式的关键。
浏览器自动切换到恰当的呈现模式,以便正确显示由doctype声明所指定的文档种类;
DOCTYPE 指定了HTML 文档遵循的文档类型定义(DTD) -->
<struts>
<package name="song" extends="struts-default">
<action name="input" class="song.action.user.InputAction">
<result name="success">/login/success.jsp</result>
<result name="error">/login/error.jsp</result>
</action>
</package>
</struts>
4>创建java;
package song.action.user;
import com.opensymphony.xwork2.Action;
/*
* 当我们在写action的时候,可以实现action接口,也可以继承actionsupport这个类.到底这2个有什么区别呢...
action接口有:
public static final java.lang.String SUCCESS = "success";
public static final java.lang.String NONE = "none";
public static final java.lang.String ERROR = "error";
public static final java.lang.String INPUT = "input";
public static final java.lang.String LOGIN = "login";
public abstract java.lang.String execute() throws java.lang.Exception;
而actionsupport这个工具类在实现了action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之前执行,如校验失败,统能将视图转入input处,必须在配置该Action时配置input属性。
另外,actionsupport还提供了一个getText(String key)方法还实现国际化,该方法从资源文件上获取国际化信息.
*/
public class InputAction implements Action {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
System.out.println("用户名:" + username + "密码:" + password);
if ("song".equals(username) && "111".equals(password)) {
return SUCCESS;
} else {
return ERROR;
}
}
}
5>创建jsp页面;
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<s:form action="input.action" method="post">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="确定" align="center"></s:submit>
<s:reset value="重置" align="center"></s:reset>
</s:form>
</center>
</body>
</html>
success.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
你输入的内容是: <br>
用户名:<font color="red"><s:property value="username"/></font><br>
密码:<font color="red"><s:property value="password"/></font>
</body>
</html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
登录失败-错误的用户名/密码<br>
用户名:<font color="blue"><s:property value="username"/></font><br>
密码:<font color="blue"><s:property value="password"/></font>
</body>
</html>
如图示: