Model-driven action usage in STRUTS2

Source: Internet
Author: User

Struts2 action belongs to the MVC model layer, the method in action represents the business logic, the property in action represents the parameter in the request, and when the page request parameter is more, Defining the properties of too many parameter objects in action does not conform to the loose coupling principle advocated by struts , so we recommend that you use JavaBean to encapsulate the parameters individually and assign values to JavaBean in action . This is the action of Modeldriven.

The model-driven action requires the action to implement the Modeldriven interface, and if the login page needs to transfer the parameters name and password, we enclose the 2 parameters in a javabean of the data, Then define the JavaBean as model in the action.

Note: The name of the member variable in the JavaBean and the Name property of the landing label are consistent.

The code is as follows:

UserInfo (Java Bean) code

 Packagemodel; Public classUserInfo {PrivateString name; PrivateString password;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }    }

HTML Landing page main code

<HTML>  <Body>    <formAction= "./user/getuser.action">User name:<inputtype= "text"name= "Name"/></P>Password:<inputtype= "Password"name= "Password"/></P>        <inputtype= "Submit"name= "Submit"value= "Submit"/>    </form>  </Body></HTML>

Modelaction Package modelaction

Importmodel. UserInfo;ImportCom.opensymphony.xwork2.ActionSupport;ImportCom.opensymphony.xwork2.ModelDriven; @SuppressWarnings ("Serial") Public classModelactionextendsActionsupportImplementsModeldriven<userinfo> {        //private UserInfo model = new UserInfo ();    PrivateUserInfo UserInfo =NewUserInfo (); @Override PublicString Execute ()throwsException {System.out.println ("Modelaction. Execute () "); return"Success"; }
@Override PublicUserInfo Getmodel () {//TODO auto-generated Method Stub returnUserInfo; }}

Struts.xml

<?XML version= "1.0" encoding= "UTF-8"?> <!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>    < Packagename= "User"namespace= "/user"extends= "Struts-default">                <Actionname= "GetUser"class= "Modelaction.modelaction">            <resultname= "Success">/myjsp.jsp</result>        </Action>            </ Package></Struts>

User name password is displayed in the action jump page myjsp.jsp

When the action is requested, the parameters in the request are automatically populated into the properties of the model UserInfo, and of course the parameter name and property name are required, and the Struts2 tag <s:property value= "username" is used on the page to jump/> You can remove the attribute username from the model userinfo. The method Getmodel () in the Modeldriven interface must be implemented, which tells the system model what the specific object is.

<%@ Page Language="Java"Import="java.util.*"pageencoding="iso-8859-1"%><%@ taglib URI="/struts-tags"prefix="s"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>  <Head>    <title>My JSP ' myjsp.jsp ' starting page</title>    <Metahttp-equiv= "Pragma"content= "No-cache">    <Metahttp-equiv= "Cache-control"content= "No-cache">    <Metahttp-equiv= "Expires"content= "0">        <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3">    <Metahttp-equiv= "description"content= "This is my page">    <!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > -  </Head>    <Body>Username:<S:propertyvalue= "Name" />  </Body></HTML>

Model-driven action usage in STRUTS2

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.