Struts2 Convention-plugin

Source: Internet
Author: User

Import this plug-in, the role of the plugin is to replace the Struts.xml principle is not configured, are all conventions

Basic steps

  1. New Homeaction, there is an Execute method return success, request home.action so that can execute this

    Convention: Home is the Name property value of the action in the previous configuration, and no method name is the default execute

Homeaction→home.action
Studenthomeaction→student-home.action

Package name, the requested action must be placed in the action,actions,strut,struts
Com.kaishengit.action.homeaction→/home.action
Com.kaishengit.action.product.homeaction→/product/home.action
Com.kaishengit.action.product.type.homeaction→/product/type/home.action

  2. Add a JSP file. The file name is defined as home-success.jsp, so you can get to the JSP. It must be in a content folder, but you can modify this folder

<constant name= "Struts.convention.result.path" value= "/web-inf/views/"/>

/home.action success→/web-inf/content/home-success.jsp
/home.action input→/web-inf/content/home-input.jsp
/product/home.action success→/web-inf/content/product/home-success.jsp

3. For the request method and the jump way to rely on annotations to achieve

Define another method in Homeaction
@Action ("main") plus annotations, add main-success.jsp request Locallhost/main.action

@Action ("main") public String main () {    return  SUCCESS;}

Jump
For the params, the odd number represents the key, even the value of the

@Action (value= "Login", results={@Result (name= "Success", type= "Redirectaction", params={"namespace", "/admin", "ActionName", "main"}), @Result (name= "Input", type= "Redirectaction", params={"namespace", "/admin", "ActionName", "Home", "Error", "1001"})            })  PublicString Login () {Admin curradmin=adminservice.login (admin); if(Curradmin = =NULL) {          returnINPUT; } Else{putsession ("Curr_admin", curradmin); returnSUCCESS; }  }

4. For the main configuration file, Struts.xml constant settings, interceptors, global-result or to configure their own

However, for filters or something, the Convention is a default package. So change it.
<constant name= "Struts.convention.default.parent.package" value= "Basepackage"/>

5. Namespaces

Com.kaishengit.action.product.homeaction→/product/home.action
Can com.kaishengit.action.homeaction→/product/home.action
To annotate @namespace ("product") on the Homeaction
At this time the request arrives the JSP all to be in/content/product/xxxx

Struts2 Convention-plugin

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.