需要的jar包文件:
index.jsp源码:
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>请输入您的注册信息</title> </head> <body> <H1>请输入您的注册信息</H1> <s:form action="regist"> <s:textfield label="用户名" name="userName"/> <s:password label="密码" name="password"/> <s:password label="请再输入一次" name="rePassword"/> <s:submit value="注册"></s:submit> </s:form> </body> </html>
welcome.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> </head> <body> 恭喜您,注册成功! </body> </html>
web.xml源码:
<?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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></web-app>
struts.xml源码:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="lee" extends="struts-default"> <action name="regist" class="tom.jiafei.ZhuceAction"> <result name="success">/welcome.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
ZhuceAction.java源码:
package tom.jiafei; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class ZhuceAction extends ActionSupport { private String userName; private String password; private String rePassword; 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 String execute() throws Exception { return SUCCESS; } }
ZhuceAction-validation.xml源码:
<?xml version="1.0" encoding="GBK"?> <!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="userName"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>密码不能为空</message> </field-validator> </field> <field name="rePassword"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>确认密码不能为空</message> </field-validator> <field-validator type="fieldexpression"> <param name="expression"><![CDATA[(password==rePassword)]]></param> <message>两次输入密码不一致</message> </field-validator> </field> </validators>
当提交的是空表单时运行的结果为:
当两次提交的密码不一致时,运行的结果为:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-10 13:37:11