下面提供一个输入校验的一个实例,递进式学习:
首先,建立输入页面:register.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% 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 ‘register.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> <table align="center" width="40%" border="0"> <tr> <td> <s:actionerror cssStyle="color:red"/><%--actionerror中增加的所有信息显示到页面上 --%> __________________________________________________ <s:fielderror cssStyle="color:blue"></s:fielderror> </td> </tr> </table> <form action="register.action" method="post"> <table align="center" width="40%" border=1"> <tr> <td> username: </td> <td> <input type="text" name="username" size="20"> </td> </tr> <tr> <td> password: </td> <td> <input type="password" name="password" size="20"> </td> </tr> <tr> <td> re-password: </td> <td> <input type="password" name="repassword" size="20"> </td> </tr> <tr> <td> age: </td> <td> <input type="text" name="age" size="20"> </td> </tr> <tr> <td> birthday: </td> <td> <input type="text" name="birthday" size="20"> </td> </tr> <tr> <td> graduation: </td> <td> <input type="text" name="graduation" size="20"> </td> </tr> <tr> <td> <input type="submit" value="submit"/> </td> <td> <input type="reset" value="reset"/> </td> </tr> </table> </form> </body> </html>
接着建立处理类RegisterAction:
package com.test.action; import java.util.Calendar; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction 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; } @Override public String execute() throws Exception { System.out.println("execute invoked"); return SUCCESS; } public String test() throws Exception { System.out.println("test invoked"); return SUCCESS; } public void validateTest() { } @Override public void validate() { System.out.println("validate~~~~~~~~~~~~~~~~"); if(null == username || username.length() < 6 || username.length() > 10) { // 进行错误消息输出 this.addActionError("username invalid"); this.addFieldError("username", "field error: username invalid"); } if(null == password || password.length() < 6 || password.length() > 10) { this.addActionError("password invalid"); } else if(null == repassword || repassword.length() < 6 || repassword.length() > 10) { this.addActionError("repassword invalid"); } else if(!password.equals(repassword)) { this.addActionError("two passwords not the same"); } if(age < 1 || age > 150) { this.addActionError("age invalid"); // this.addFieldError("age", "field error: age invalid"); } if(null == birthday) { this.addActionError("birthday invalid"); } if(null == graduation) { this.addActionError("graduation invalid"); } if(null != birthday && null != graduation) // 可以不写,因为前面验证通过了 { Calendar c1 = Calendar.getInstance(); c1.setTime(birthday); Calendar c2 = Calendar.getInstance(); c2.setTime(graduation); if(!c1.before(c2)) { this.addActionError("birthday should be before graduation"); } } } }
success.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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> <table align="center" width="40%" border=1"> <tr> <td> username: </td> <td> ${requestScope.username } </td> </tr> <tr> <td> password: </td> <td> ${requestScope.password } </td> </tr> <tr> <td> age: </td> <td> ${requestScope.age } </td> </tr> <tr> <td> birthday: </td> <td> ${requestScope.birthday } </td> </tr> <tr> <td> graduation: </td> <td> ${requestScope.graduation } </td> </tr> </table> </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"> <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.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts2" extends="struts-default"> <action name="helloworld" class="com.test.action.HelloWorld"> <result name="success">/helloworld.jsp</result> </action> <action name="login" class="com.test.action.LoginAction"> <result name="success">/result.jsp</result> </action> <action name="converterAction" class="com.test.action.PointAction" method="test"> <result name="success">/output.jsp</result> </action> <action name="register" class="com.test.action.RegisterAction" method="test"> <result name="success">/success.jsp</result> <result name="input">/register.jsp</result> </action> </package> </struts>
为了实现本地化,在RegisterAction.java所在的包com.test.action下新建一个RegisterAction.properties文件,通过Add的方式添加以下内容:
访问http://localhost:8080/struts2/register.jsp,这里是一个输出案例:
这里需要注意的几点:校验方法中自己定义的校验方法首先执行,最后执行validate方法;Action级别的消息struts是不会帮助我们自动生成的,而field级别的错误消息会帮助我们生成,同时还可以写自己的错误消息,这是field级别的struts定义的和字节定义的消息都会输出;为了在页面上输出信息,需要在form表单上面添加类似下面这样的信息:
<s:actionerror cssStyle="color:red"/><%--actionerror中增加的所有信息显示到页面上 --%>
<s:fielderror cssStyle="color:blue"></s:fielderror>
时间: 2024-11-05 20:38:51