Part 12th _STRUTS2 Input Check

Source: Internet
Author: User

An example of input validation is provided below, with progressive learning:

First, create the input page: 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" >

The

Then establishes the processing class 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 St Ring 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.) {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;} @Overridepublic 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 () {} @Overridepublic void Validate () {System.out.println ("validate~~~~~~~~~~~~~~~~"); if (null = = Username | | Username.length () < 6 | | Username.length () > 10) {//error message Output This.addactionerror ("username invalid"); This.addfielderror ("username", "field Error:username invalid ");} if (null = = password | | password.length () < 6 | | password.length () >) {this.addactionerror ("password Invalid");} else if (null = = Repassword | | repassword.length () < 6 | | repassword.length () >) {this.addactionerror ("Repassword I Nvalid ");} else if (!password.equals (Repassword)) {This.addactionerror ("both passwords not the same");} if (age < 1 |-age > Max) {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)//Can not be written because the previous validation passed {Calendar C1 = calendar.getinstance (); C1.settime (Birt Hday); Calendar C2 = calendar.getinstance (), C2.settime (Graduation), if (!c1.before (C2)) {This.addactionerror ("Birthday Should be before graduation ");}}}

The

success.jsp is as follows:

<%@ 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" >

The Web. xml file is configured as follows:

<?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>

The Struts.xml configuration is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.org/dt Ds/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> 

To localize, create a new Registeraction.properties file under the package com.test.action where Registeraction.java is located, adding the following via add:

  

To access http://localhost:8080/struts2/register.jsp, here is an output case:

Here are a few things to note: The calibration method defined in the method of calibration is executed first, the last execution of the Validate method, and the action-level message struts does not help us to generate automatically, and the field-level error message will help us build, but also can write their own error message, This is the field-level struts-defined and byte-defined messages are output; In order to output information on a page, you need to add information like the following on the form form:

<s:actionerror cssstyle= "color:red"/><%--actionerror add all the information displayed on the page--%>
<s:fielderror cssstyle= "Color:blue" ></s:fielderror>

  

Part 12th _STRUTS2 Input Check

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.