此处为了简便起见和知识重复性的避免,暂不考虑类型转换问题。
第一步:(这一步和其他一样,这里从简)依旧是新建一个web project,命名为checkxml,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。
第二步:将index.jsp改名为reg.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。Input.jap的代码如下
第二步:action
在src目录下新建新建包cn.edu.hdu.action 在其中新建一个RegAction.java文件
代码如下
第三步:修改struts.xml文件 代码如下
第四步: 在WebRoot目录下新建另一个视图 success.jsp 代码如下(此文件在这个效验中基本没有什么作用)
最后一步,也是本节真正内容所在
在cn.edu.hdu.action目录下新建一个名字与action对应的xml文件
RagAction-validation.xml 代码如下:
第一步:(这一步和其他一样,这里从简)依旧是新建一个web project,命名为checkxml,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。
第二步:将index.jsp改名为reg.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。Input.jap的代码如下
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.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> <s:form action="reg"> <s:textfield name="username" label="username"></s:textfield> <s:password name="password" label="password"></s:password> <s:password name="repassword" label="repassword"></s:password> <s:textfield name="age" label="age"></s:textfield> <s:textfield name="birthday" label="birthday"></s:textfield> <s:textfield name="graduation" label="graduation"></s:textfield> <s:submit name="submit"></s:submit> <s:reset name="reset"></s:reset> </s:form> </body> </html>
第二步:action
在src目录下新建新建包cn.edu.hdu.action 在其中新建一个RegAction.java文件
代码如下
package cn.edu.hdu.action;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegAction extends ActionSupport
{
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
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;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
public String execute() throws Exception
{
return SUCCESS;
}
}
第三步:修改struts.xml文件 代码如下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="checkxml" extends="struts-default"> <action name="reg" class = "cn.edu.hdu.action.RegAction"> <result name="success">/success.jsp</result> <result name="input">reg.jsp</result> </action> </package> </struts>
第四步: 在WebRoot目录下新建另一个视图 success.jsp 代码如下(此文件在这个效验中基本没有什么作用)
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% 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> 恭喜你 验证通过 </body> </html>
最后一步,也是本节真正内容所在
在cn.edu.hdu.action目录下新建一个名字与action对应的xml文件
RagAction-validation.xml 代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "//www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <!-- 框架校验部分 --> <validators> <!-- 分别对每一个字段校验 --> <field name="username"> <!-- 每一种校验标准 --> <field-validator type="requiredstring"> <!-- field-validator下的一些属性标签 说白了 就是一些传入函数的参数 --> <param name="trim">true</param> <!-- 出错信息 --> <message>username is required</message> </field-validator> </field> <!-- 一下都和username类似 将不做批注 --> <field name="password"> <!-- 每一种校验标准 --> <field-validator type="requiredstring"> <!-- field-validator下的一些属性标签 说白了 就是一些传入函数的参数 --> <param name="trim">true</param> <!-- 出错信息 --> <message>password is required</message> </field-validator> <field-validator type="stringlength"> <param name ="minLength">6</param> <param name ="maxLength">10</param> <message> password should between ${minLength} to ${maxLength} </message> </field-validator> </field> <field name="repassword"> <!-- 每一种校验标准 --> <field-validator type="requiredstring"> <!-- field-validator下的一些属性标签 说白了 就是一些传入函数的参数 --> <param name="trim">true</param> <!-- 出错信息 --> <message>repassword is required</message> </field-validator> <field-validator type="stringlength"> <param name ="minLength">6</param> <param name ="maxLength">10</param> <message> repassword should between ${minLength} to ${maxLength} </message> </field-validator> </field> <field name="age"> <!-- 每一种校验标准 --> <field-validator type="int"> <param name ="min">1</param> <param name ="max">150</param> <message>age should between ${min} to ${max}</message> </field-validator> </field> <field name="birthday"> <!-- 每一种校验标准 --> <field-validator type="requiredstring"> <!-- field-validator下的一些属性标签 说白了 就是一些传入函数的参数 --> <param name="trim">true</param> <!-- 出错信息 --> <message>birthday is required</message> </field-validator> <field-validator type="date"> <param name ="min">2000-1-1</param> <param name ="max">2010-12-31</param> <message>birthday should between ${min} to ${max}</message> </field-validator> </field> <field name="graduation"> <!-- 每一种校验标准 --> <field-validator type="requiredstring"> <!-- field-validator下的一些属性标签 说白了 就是一些传入函数的参数 --> <param name="trim">true</param> <!-- 出错信息 --> <message>graduation is required</message> </field-validator> <field-validator type="date"> <param name ="min">2000-1-1</param> <param name ="max">2010-12-31</param> <message> graduation should between ${min} to ${max} </message> </field-validator> </field> </validators>