Simple struts2 Action model & form submission demo and upgrade to STRUTS2 2.5

Source: Internet
Author: User

Simple struts2 Action model & form submission, inheriting Actionsupport, the class that the Struts.xml defines action points to to accept submissions from.

The relevant or required documentation is as follows: web.xml need to add struts2 filter Definitions

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>sfmisStruts2< /display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file >index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> Default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file> default.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> &Lt;url-pattern>/*</url-pattern> </filter-mapping> </web-app> 

SRC root directory struts.xml file, defined action defined 3 Action,index, register_input and register, respectively, are welcome interface, input interface and display interface. Action Name= "Action name" class= "Process Class" Method= "class method"
This segment of the XML in <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/dtds/ Struts-2.0.dtd "> can not be omitted, otherwise not normal.

<?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.i18n.encoding "value=" Utf-8 "/> < Constant Name= "Struts.locale" value= "Zh_cn"/> <constant name= "Struts.devmode" value= "true"/> <constant na Me= "Struts.ui.theme" value= "simple"/> <package name= "basicstruts2" extends= "Struts-default" namespace= "> <!--If No class attribute is specified the framework would assume success and render the result index.jsp--&G
		T <!--If No Name value for the result node is specified the success value is the default--> <action name= "index "> <result>/index.jsp</result> </action> <action name=" Register_input "class=" Struts.hello World.action.Register "method=" input "> <result name=" Input ">/register.jsp</result>;/action> <!--for logger interceptor see:http://struts.apache.org/2.2.1/docs/logger-interceptor.html--> & lt;! --For Timer interceptor http://struts.apache.org/2.2.1/docs/timer-interceptor.html--> <action name= " Register "class=" Struts.helloworld.action.Register "method=" execute "> <interceptor-ref name=" Timer "/> < Interceptor-ref name= "Logger"/> <interceptor-ref name= "Defaultstack" > <param name= "exception.logEnable" D ">true</param> <param name=" Exception.loglevel ">ERROR</param> </interceptor-ref>
		; result name= "Success" >/thankyou.jsp</result> <result name= "Input" >/register.jsp</result> </action> </package> </struts>

The web.xml definition for struts2.5 differs from that of the previous version, with 2.5 web.xml defined as follows:

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class> Org.apache.struts2.dispatcher.filter.strutsprepareandexecutefilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</ Url-pattern>
	</filter-mapping>

The head of struts2.5 's Struts.xml file is also different, notice the difference:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.5//en" "http://struts.apache.org/dtds/ Struts-2.5.dtd ">

Struts Action:Register.javaInherits the Actionsupport,setpersonbean method to receive form submissions, which is defined by the Struts.xml.

Package struts.helloworld.action;
Import Java.text.DateFormat;
Import Java.util.Date;
Import Struts.helloworld.model.Person;

Import Com.opensymphony.xwork2.ActionSupport;
  public class Register extends Actionsupport {private static final long serialversionuid = 1L;

  private person Personbean; Public String Execute () throws Exception {//Call Service class to store Personbean ' s state in Database return
  SUCCESS; public void Validate () {if (Personbean.getfirstname (). Length () = 0) {addfielderror ("personbean.fi
    Rstname "," the name is required. ");
    } if (Personbean.getemail (). Length () = 0) {addfielderror ("Personbean.email", "email is required."); } if (Personbean.getage () <) {Addfielderror ("Personbean.age", "age is required and must")
    Er ");
  Getpersonbean () {return personbean;
  public void Setpersonbean {personbean = person;
}
}
 

Strust Model:Person.java

A Form object, which can be said to be a data object, a field that corresponds to a data table, or a fields information corresponding to the from submission.

package Struts.helloworld.model;
 /** * Models a who registers.
  * * * @author Bruce Phillips * * * * * * * */public class Person {private String firstName;
  Private String LastName;
  Private String Email;

  private int age;
  Public String Getfirstname () {return firstName;
  } public void Setfirstname (String firstName) {this.firstname = FirstName;
  Public String Getlastname () {return lastName;
  } public void Setlastname (String lastName) {this.lastname = LastName;
  Public String Getemail () {return email;
  public void Setemail (String email) {this.email = email;
  public int getage () {return age;
  public void Setage (int age) {this.age = age;      Public String toString () {return "A-name:" + getfirstname () + "last Name:" + getlastname () + "Email:
  "+ getemail () +" Age: "+ getage (); }
}

Web root directory: index.jsp Welcome page, showing registration page for register entry

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

Web root/directory: register.jsp corresponds to the Personbean defined by private person within Register.java.

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

Web root/directory: thankyou.jsp display results.

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

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.