1.用户注册页面register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<form action="register.action" method="post">
<I><font face="楷体_GB2312" color="#FF0000"><s:fielderror />
</font> </I>
<!-- 读取显示提示信息 -->
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="user.userName">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="user.password">
</td>
</tr>
<tr>
<td>
确认密码:
</td>
<td>
<input type="password" name="user.rePassword">
</td>
</tr>
<tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</table>
</form>
</body>
</html>
2.注册成功result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<center><h7><b>Result!</b></h7></center>
<br>
<center>
${user.userName} <br>
${user.password} <br>
</center> </html>
3.注册处理action RegisterAction
package com.zyl.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.zyl.bean.User;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private User user;
@Override
public String execute() throws Exception {
if(!(user.getPassword().equals(user.getRePassword()))){
this.addFieldError("password", "请输入相同的密码");
return "input";
}
else
{
HttpServletRequest request = ServletActionContext.getRequest ();
request.setAttribute("user", user);
return SUCCESS;
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
4. 用户Bean User.java
package com.zyl.bean;
import java.util.Date;
public class User {
private String userName;
private String password;
private String rePassword;
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 String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
5.配置验证文件RegisterAction-validation.xml
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="user.userName">
<!-- 验证字符串不能为空 -->
<field-validator type="requiredstring">
<!-- 去空格 -->
<param name="trim">true</param>
<!-- 错误提示信息 -->
<message>用户名不能为空</message>
</field-validator>
<!-- 验证字符串长度 -->
<field-validator type="stringlength">
<param name="minLength">2</param>
<param name="maxLength">20</param>
<message>用户名长度应在2到18个字符间</message>
</field-validator>
</field>
<field name="user.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">18</param>
<message>密码长度应在6到18个字符之间</message>
</field-validator>
</field>
</validators>
6.struts2框架默认加载的配置文件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">
<struts>
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
<package name="user" extends="struts-default">
<action name="register" class="com.zyl.action.RegisterAction">
<result name="success">/result.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package>
</struts>
7.web服务器启动时加载Struts 配置文件 web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
</web-app>
structs2表单验证
原创mb6444ed45406a4 ©著作权
©著作权归作者所有:来自51CTO博客作者mb6444ed45406a4的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Hibernate 配置
下一篇:hibernate 的简单创建
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
structs2基本程序创建
1. 使用的工具eclipse-jee-luna-SR2-win32
java struts xml -
structs2拦截器详解
Struts2(XWork)提供的拦截器的功能说明: 拦截器
structs2 拦截器 -
Structs2中配置文件详解
Struct2-配置文件:整个配置我们可以分为两大块,一个是在web.xm
struts2 struts xml xml文件 -
structs2源码解读(6)之解析package标签
这篇博文解析了struct2解析struct*.xml配置文件中package标签的具体过程
strut2 解析package标签 -
structs2通配符映射There is no Action mapped for namespace
在使用struts2.5.8的通配符配置请求URL的时候,报错There is no Action mappe
struts2.0 struts 访问权限 解决方法