The basic situation of strust

Source: Internet
Author: User

Advantages of Struts:

1, the implementation of MVC pattern, structure clear

2, a rich tag (tag)

3, through the configuration file page navigation, facilitates the post-maintenance

4. loosely coupled with Servlet API for easy testing

Structs2=structs1 's reputation and market +webwork technology

Ii. acquisition of Struts2

1, http://struts.apache.org/download.cgi

2, Struts-2.3.16.3-all.zip

Iii. Main catalogue of STRUTS2

Apps : This folder contains a sample app based on Struts2

Docs: This folder contains Struts2 related documents, including Struts2 QuickStart ,Struts2 documentation, and API documentation. content

Lib : This folder contains the core class library for the STRUTS2 framework, and

Struts2 third-party plug-in class library

SRC : This folder contains all the source code for the STRUTS2 framework

Iv. Struts2 Build the required jar Package

Commons-fileupload-1.3.1.jar:Struts file upload and download

Commons-io-2.2.jar: File Read

Commons-lang3-3.1.jar: provides extensions for Java.lang packages

Freemarker-2.3.19.jar:freemarker is a template engine, a generic tool (. FTL) that generates text output based on a template

Ognl-3.0.6.jar: Supports ognl expressions

Javassist-3.11.0.ga.jar: Parsing, editing, and creating JAVA Bytecode class libraries (you can override . class files)

Core pack of Struts2-core-2.3.16.3.jar:struts2 (webwork)

Core pack of Xwork-core-2.3.16.3.jar:xwork (webwork)

Asm-*.jar : similar to javassits

Log4j-1.2.17.jar: Log Class library

There are three ways to implement Action:

1. Create your own Java class: Add a method:

Public String execute () {

Return "Success";

}

Configure Struts2.xml

<package name = "Default" namespace= "/" extends= "Struts-default"

<action name = "Hello" class= "..." >

<result>hello.jsp</result>

</action>

</package>

2. Implement Interface Action

3. inheriting actionsupport

Action receive parameter (attribute mode)

//to receive user logins and passwords entered by the user as an attribute Public classUseractionextendsActionsupport {PrivateString UserName;//User name    PrivateString password;//Password     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; } @Override PublicString Execute ()throwsException {return"Success"; }    }

Control business Logic

The name of the form parameter must have a corresponding setter in the Action and a getter

Fix garbled

Modify struts.xml, add code <constantname= "struts.i18n.encoding" value= " encoded format "/>

Page display

<% @taglib uri= "/struts-tags" prefix= "s"%>

<s:property value= " property name "/>

Application Scenarios

when the number of parameter fields received is small, for example, for the search application, receive user input search criteria

Action receive parameter (JavaBean mode): Separates model number from Action

//the user name and password entered by the user login are received in JavaBean mode. Public classUserAction2extendsActionsupport {Privateuser User;  PublicUser GetUser () {returnuser; }     Public voidsetUser (user user) { This. user =user; } @Override PublicString Execute ()throwsException {return"Success"; }}

Define entity classes, add attributes to Entities, and setter and getter

Com.pb.entity.User

Add entity attributes and setter and getter to Action

User User

Modify the form parameter name to be the entity object name . Property name

<input type= "text" name= "User.username"/>

Value is passed <s:property value= " object name . the form of the property name "/>"

<s:property value= "User.username"/>

How the action receives parameters

Use the method property  

Access Action , the Execute () method is called by default

Multiple business methods can be defined in Action

Method signatures are the same as the Execute () method

Configure multiple <action> tags

 method property specifies the method called  public  class  houseaction         Actionsupport { public   String Add () {     return  "Success" ;  public   String update () { 
   
    return  "Success" 
    ; 
}}
<name= "House_add"  class= "Com.pb.web.action.HouseAction " Method = "Add" > <  name= "House_update"  class= "Com.pb.web.action.HouseAction"  method= "Update" >

Use dynamic method calls (closed by default)

Dynamic Method Invocation

DMI(dynamic method invocation)

There can be more than one business method in an Action class

 Public class extends Actionsupport {    public  String Add () {        return "Success";    public  String Update () {        return ' success ';    }}        

Only need to configure a <action> tag

You do not need to use the method property

Call to indicate Action name and business method

<action name= "House" class= "Com.pb.web.action.HouseAction" >

Call to indicate Action name and business method

Http://localhost:8080/struts2/house!add

the name property of <result>

jump to a different result view based on the return value of the Action business method

 Public throws Exception {  return "addsuccess";}  Public throws Exception {  return "updatesuccess";}
<Actionname= "House"class= "Com.pb.web.action.HouseAction" >    <resultname= "Addsuccess">/houseaddsuccess.jsp</result>    <resultname= "Updatesuccess">/houseupdatesuccess.jsp</result></Action>

The basic situation of strust

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.