struts2-15-users of the famous school inspection

Source: Internet
Author: User

The check is divided into:

1:javascript Regular Expressions

2:javaee in Validation ()

A: User registration information userdetails.jsp

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <%@ taglib Prefix="s"URI="/struts-tags"%>4 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">5 <HTML>6 <Head>7 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">8 <title>Registration page</title>9 </Head>Ten <Body> One <S:fielderror></S:fielderror> A     <formAction= "Userdetailsaction"Method= "POST"> -User name:<inputtype= "text"name= "username" /><BR>  -Birthday:<inputtype= "text"name= "Birthday" />(In the format: YYYY-MM-DD)<BR>  theDetailed Address:<inputtype= "text"name= "Address">Province&nbsp;&nbsp; -                <inputtype= "text"name= "Address">City&nbsp;&nbsp; -                <inputtype= "text"name= "Address">Area<BR> -                <inputtype= "text"name= "Address"/>         +         <inputtype= "Submit"value= "Register"> -     </form> + </Body> A </HTML>

Two: Write the set get method in action nuc.sw.action--->userdetailsaction.java

 1 package nuc.sw.action; 2 import java.util.Date; 3 import com.opensymphony.xwork2.ActionSupport; 4 5 Import NUC.SW . Vo. Address;  6 public class Userdetailsaction extends actionsupport{7 private String username; 8//Note Type 9 private Date birthday;10//Must be an address type. Otherwise, it cannot be obtained. One private Address address;12 public Date getbirthday () {return birthday;14}15 public void Setbirthday (Date birthday) {this.birthday = birthday;17}18 public String GetUserName () {RET  Urn username;20}21 public void Setusername (String username) {this.username = username;23}24 25 26 Public Address getaddress () {address;29}30, public void setaddress (address address ) {this.address = address;34}35-Notoginseng public String Registermethod () throws Exception {Retu RN success;39}40} 

Three: Get address type so write Address.java--->nuc.sw.vo

Package Nuc.sw.vo;public class Address {    private String province;    Private String city;    Private String County;    Private String Street;    Public Address (String province, String city, String county, String street) {        super ();        this.province = Province;        this.city = City;        This.county = county;        This.street = Street;    }    Public String getprovince () {        return province;    }    public void Setprovince (String province) {        this.province = province;    }    Public String getcity () {        return city;    }    public void Setcity (String city) {        this.city = city;    }    Public String Getcounty () {        return county;    }    public void Setcounty (String county) {        this.county = county;    }    Public String Getstreet () {        return street;    }    public void Setstreet (String street) {        this.street = Street;    }}

Four: Configuration Struts.xml

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE struts Public 3     "-//apache software foundation//dtd struts Configuration 2.3//en" 4     "/http Struts.apache.org/dtds/struts-2.3.dtd "> 5  6 <struts> 7  <constant name=" Struts.devmode "value=" True "/> 8  <package name=" Default "namespace="/"extends=" Struts-default "> 9      <action name=" Userdetailsaction "class=" nuc.sw.action.UserDetailsAction "method=" Registermethod ">10          <result>/ showuserdetails.jsp</result>11          <result name= "input" >/userdetails.jsp</result>12      </ action>13  </package>14 </struts>

V: After the user submits the information to display showuserdetails.jsp

The month of the birthday requires mm---mm finger time

1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2     pageencoding=" Utf-8 "%> 3 <%@ taglib prefix=" s "uri="/struts-tags "%> 4 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 5 

Six: The submitted address information is a string, the last to be output as an object, so you need to customize the converter

Nuc.sw.action--->addressconverter.java

 1 package nuc.sw.action; 2 3 Import Java.util.Map; 4 5 Import Org.apache.struts2.util.StrutsTypeConverter; 6 Import nuc.sw.vo.Address; 7 public class Addressconverter extends Strutstypeconverter {8 9/*10 * 1: Converter must inherit StrutsTypeConverter11 * 2: From Take method string to value (address) 12 * 3: Self-bringing method value to string */14 @Override15 public Object convertfromstring (Map a RG0, string[] arg1, Class arg2) {16 17//Here can be abbreviated code, but must be in Address.java write construction method Address Address=new ADD Ress (Arg1[0],arg1[1],arg1[2],arg1[3]); 4 values in address in//string[]//Address.setprovince (arg1[0]); 22/         /address.setcity (arg1[1]);//Address.setcounty (arg1[2]); +//Address.setstreet (arg1[3]); 25         Return address;26}27 @Override30 public String converttostring (Map arg0, Object arg1) {31 AG1 is the parent class, address is a subclass and requires casting. if (arg1 instanceof address) {address a= (address) arg1;34 return a. Getprovince () + "province" +a.getcity () + "city" +a.getcounty () + "district" +a.getstreet ();}36 else37 return null;38 }39 40}

Seven: Local Converter---Write the profile of the property name in the action---and action in the sibling directory

Address=nuc.sw.action.addressconverter

Eight: Verify the user name Nuc/sw/action--->userdetailsaction-validation.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2   <!DOCTYPE validators Public3 "-//apache struts//xwork Validator 1.0.2//en"4 "Http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">5  <validators>6    <Fieldname= "username">7        <Field-validatortype= "Requiredstring">8          <paramname= "Trim">True</param>9          <message>User name cannot be empty</message>Ten        </Field-validator> One        <Field-validatortype= "Regex"> A          <paramname= "Regexexpression">^\w{4,25}$</param> -          <message>The user name must be 4-25-bit</message> -        </Field-validator> the    </Field> -  </validators>

Nine: Project structure:

Ten: Running results

struts2-15-users of the famous school inspection

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.