1.输入页面login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<!--
<table align = "center">
<tr>
<td>
<s:fielderror cssStyle="color:red" />
</td>
</tr>
</table>
-->
<!-- 表单重复提交的错误信息放在了actionerror中 -->
<s:actionerror/>
<s:form action="login" method = "post" theme = "simple" >
<s:token></s:token><!--防止表单重复提交-->
<table align="center" border="1" width="60%">
<tr>
<td >
username
</td>
<td>
<s:textfield name="username" cssStyle="Color:red"></s:textfield>
</td>
<td style="color:red">
<s:fielderror>
<s:param>username</s:param>
</s:fielderror>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<s:password name="password"></s:password>
</td>
<td style="color:red">
<s:fielderror>
<s:param>password</s:param>
</s:fielderror>
</td>
</tr>
<tr>
<td>
repassword
</td>
<td>
<s:password name="repassword" label="repassword"></s:password>
</td>
<td style="color:red">
<s:fielderror>
<s:param>repassword</s:param>
</s:fielderror>
</td>
</tr>
<tr>
<td>
age
</td>
<td>
<s:textfield name="age" label="age"></s:textfield>
</td>
<td style="color:red">
<s:fielderror>
<s:param>age</s:param>
</s:fielderror>
<tr>
<td>
birthday
</td>
<td>
<s:textfield name="birthday" label="birthday"></s:textfield>
<td style="color:red">
<s:fielderror>
<s:param>birthday</s:param>
</s:fielderror>
<tr>
<td>
graduation
</td>
<td>
<s:textfield name="graduation" label="graduation"></s:textfield>
</td>
<td style="color:red">
<s:fielderror>
<s:param>graduation</s:param>
</s:fielderror>
<tr>
<td>
<s:submit label="submit"></s:submit>
</td>
<td>
<s:reset label="reset" />
</td>
</tr>
</table>
</s:form>
</body>
</html>
2.struts.xml配置文件:
<constant name="struts.custom.i18n.resources" value="message"></constant>
<action name="login" class="com.struct2.test.LoginAction">
<result name = "input">/login.jsp</result>
<result name = "login" type ="redirect">/login.jsp</result>
<result name = "success">/result.jsp</result>
<result name = "invalid.token">/login.jsp</result>
<!-- 防止表单重复提交的拦截器 -->
<interceptor-ref name="token"></interceptor-ref>
<!-- 指定/排除某些方法的拦截器,指定和排除同时存在时,指定的优先级高-->
<!-- myInterceptor3为自定义的一个拦截器,这里不再贴出来了-->
<interceptor-ref name="myInterceptor3">
<param name="includeMethods">execute,test</param> <!-- 指定-->
<param name="excludeMethods">execute,test</param><!-- 排除-->
</interceptor-ref>
<!-- 当提供自定义的拦截器后,默认拦截器失效,所以要手动包含进来-->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
3.LoginAciton.java:
package com.struct2.test;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
//get和set方法省略
@SuppressWarnings("unchecked")
public String execute(){
// if("hahadao".equals(this.getUsername().trim())&&"123456".equals(this.getPassword().trim())){
// return SUCCESS;
// }else{
// this.addFieldError("username", "username or password error"); //fielderror是map。key-value对。
// return INPUT;
// }
Map map = ActionContext.getContext().getSession();//session也为map。
map.put("password", this.getPassword());
//ServletActionContext比ActionContext能多拿到一个response,其他两者一样。
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("------u-------"+username);
Cookie cookie = new Cookie("username",this.getUsername());
cookie.setMaxAge(1000);
response.addCookie(cookie);
System.out.println(password + "-----密码确认是:"+repassword);
return SUCCESS;
}
public void test(){
}
// @Override
// public void validate() {//采用struts校验框架验证,不需要validate方法。
//
// if(null==this.getUsername()||"".equals(this.getUsername().trim())){
// this.addFieldError("username", "username required");
// }
//
// if(null==this.getPassword()||"".equals(this.getPassword().trim())){
// this.addFieldError("password", "password required");
// }
// }
}
4.LoginAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>名字不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>名字长度在${minLength}到${maxLength}之间</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>密码长度在${minLength}到${maxLength}之间</message>
</field-validator>
</field>
<field name="repassword">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码确认不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>密码确认长度在${minLength}到${maxLength}之间</message>
</field-validator>
</field>
<field name="repassword">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[repassword==password]]>
</param>
<message>密码确认要和密码一致</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>年龄应该在${min}到${max}之间</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<message>生日不能为空</message>
</field-validator>
<field-validator type="date">
<param name="min">1900-01-01</param>
<param name="max">2001-01-01</param>
<message>生日必须在${min}和${max}之间</message>
</field-validator>
</field>
<field name="graduation">
<field-validator type="required">
<message>毕业时间不能为空</message>
</field-validator>
<field-validator type="date">
<param name="min">1990-01-01</param>
<param name="max">2010-01-01</param>
<message>毕业时间必须在${min}和${max}之间</message>
</field-validator>
</field>
</validators>
5.result.jsp
<body>
username:${request.username }<br>
password:${requestScope.password }<br>
age:${requestScope.age }<br>
birthday:${request.birthday }<br>
request:<%=request.getAttribute("birthday") %><br>
cookie:${cookie.username.value}
</body>
6.国际化资源文件message.properties配置:
当表单重复提交时防止struts2在页面自动输出英文提示信息,而是自定义输出中文信息如下为(“不能重复提交表单! ”)
struts.messages.invalid.token=\u4e0d\u80fd\u91cd\u590d\u63d0\u4ea4\u8868\u5355\uff01