struts2 convention over configuration

Recently started to pay attention struts2 new features, starting from this version, Struts start using the convention-plugin instead of codebehind-plugin struts to achieve zero configuration.
Streamlined profile, the development process is really simple, but we know the configuration of a sudden disappear, and really what is incompatible. Followed the tide go and see the how to buttoned convention-plugin. Use Convention plugin, you need to make the application of JAR file into your WEB-INF/lib directory, you can in your Maven project POM file to add the following packages depend org.apache.struts struts2-convention-plugin 2.1. 6 Zero Configuration is not without configuration, but by way of agreement over allocation, agreed to dispatch a large number of pages through the jump and make configuration greatly reduced. So, first of all should understand the agreement under the convention-plugin:

1. Default, all results are stored in WEB-INF/content page, you can set
struts.convention.result.path to change the value of the property to the other path. Such as: Xml code will be under the path configured to WEB-INF/page.

2. Default package path contains action, actions, struts, struts2 struts all the packet will be as the Action class contains the path to search. You can set the struts.convention.package.locators property to modify this configuration. Such as: are defined in the project, including the path contains the web and action of the Action will be considered an existing path to search. Com.ustb.web .* / com.ustb.action .* Action will be deemed to contain the package path and be searched.

3. Then, Convention of the package and the previous step to find his son to find com.opensymphony.xwork2.Action package implementation as well as the Action at the end of the class: com.example.actions.MainAction com.example.actions.products.Display (implements com.opensymphony.xwork2.Action) com.example.struts.company.details.ShowCompanyDetailsAction

4. Namespace. From the definition. Package.locators marked the beginning to the end of the part of the package is a namespace. For example: Com.ustb.web.user.userAction namespace is: "/ user". Com.ustb.web.user.detail.UserAction namespace is: "/ user / detail"

5. Convention to adopt the following rules to determine the URL of the specific resource sections: remove the class name of the Action section. Then the first letter of each division to lowercase, use the '-' split, or if you can set struts.convention.action.name.separator example: UserAction-> user UserDetailAction -> user-detail . Combination of the above. For com.ustb.web.user.detail.UserDetailAction, map url is / WEB-INF/content/user/detail/user-detail.jsp

6. Struts support. Jsp. Html. Htm. Vm format. The following is actiong and results template mapping: URL Result File that could match Result Type / hello success / WEB-INF/content/hello.jsp Dispatcher / hello success / WEB-INF/content/hello-success.htm Dispatcher / hello success / WEB-INF/content/hello.ftl FreeMarker / hello-world input / WEB-INF/content/hello-world-input.vm Velocity / test1/test2/hello error / WEB-INF/content/test/test2 / hello-error.html Dispatcher
The content of the above documents from the struts2 http://struts.apache.org/2.1.6/docs/convention-plugin.html course, the simple way by default configured not fully meet the actual needs of the project. Fortunately, convention zero configuration is very flexible. Through the @ Action annotation to the following examples: Java code package com.example.web; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport (@ Action ("action1") public String method1 () (return SUCCESS;) @ Action ("/ user/action2") public String method2 () (return SUCCESS;)) method is called the path name of the default path to the default mapping method1 / hello! method1.action. / WEB- INF / content / hello.jsp method2 / hello! method2.action. / WEB-INF/content/hello.jsp
@ Action annotation by method name after the call path after the @ Action annotation
@ Action annotation and map the path method1 / action1! Method1.action. / WEB-INF/content/action1.jsp method1 / user/action2! Method2.action / WEB-INF/content/user/action2.jsp
Java code through the @ Actions annotation package com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; public class HelloAction extends ActionSupport (@ Actions ((@ Action ("/ different / url"), @ Action ("/ another / url"))) public String method1 () (return "error";)

We can: / different / url! Method1.action or / another / url! Method1.action to call method1 method. Corresponding mapping the path are / WEB-INF/content/different/url-error.jsp; / WEB-INF/content/another/url-error.jsp may mislead us, a method is after the @ Action annotation, but more a call, rather than covering the original call that way. For example, the following example:
Java code com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.convention.annotation.Action; import org.apache.convention.annotation.Actions; public class HelloAction extends ActionSupport (@ Action (" / another / url ") public String method1 () (return" error ";)
We call method1 method can in two ways:
1 / hello! Method1.action map url: / WEB-INF/content/hello-error.jsp 2 / another / url! Method1.action map url: / WEB-INF/content/another/url-error.jsp shows two methods method1 method can be called on, the only difference is that two kinds of calls is not the same map, so to jump to a different interface, which is a very good option. Through the @ Namespace annotation package com.example.web; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; @ Namespace (" ; / other ") public class HelloWorld extends ActionSupport (public String method1 () (return" error ";) @ Action (" url ") public String method2 () (return" error ";) @ Action (" / different / url ") public String method3 () (return" error ";))
Through the / other / hello-world! Method1.action access method1 method.
Through the / other / url! Method2.action visit method2 Methods / different / url! Method3.action visit method3 @ Action annotation methods and different is that the comment covering the default namespace (here'/'), until then with a hello! method1.action has not visited the method1.
@ Results and @ Result 1 overall (global). Overall results can be action all the action class to share, this results in the class action to declare the use of annotations.
package com.example.actions; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention . annotation.Result; import org.apache.struts2.convention.annotation.Results; @ Results ((@ Result (name = "failure", location = "/ WEB-INF/fail.jsp"))) public class HelloWorld extends ActionSupport (public String method1 () (return "failure";) @ Action ("/ different / url") public String method2 () (return "failure";)) When we visit / hello-world! method1. action, the return / WEB-INF/fail.jsp when we visit / hello-world! method2.action, the return / WEB-INF/fail.jsp when we visit / different / url! method2.action, the return / WEB -INF/fail.jsp 2 local (local). Local results can only be declared in the action method. Java code package com.example.actions; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.convention . annotation.Result; import org.apache.convention.annotation.Results; public class HelloWorld extends ActionSupport (@ Action (value = "/ other / bar", results = (@ Result (name = "error", location = "www.baidu.com", type = "redirect"))) public String method1 () (return "error";)) When we call / hello-world! method1.action, the return / WEB-INF / content / hello-error.jsp when we call / other / bar! method1.action, the return www.baidu.com
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of struts2 convention over configuration

  • Struts2 Spring Hibernate integration of easy

    1. Add Spring 2.0 in Libraries Choose the following four jar, and configure the / WEB-INF/lib under Spring2.0 AOP Libraries Spring2.0 Core Libraries Spring2.0 Persistence Core Libraries Spring2.0 WEb Libraries At the same time, the applicationContext ...

  • EJB ant script to deploy template works

    <? xml version = "1.0" encoding = "UTF-8"?> <! - Name Project name basedir build.xml file directory -> <project name="HelloWorld" basedir="."> <! - Property variables -> <! - The sour ...

  • Struts2 Spring Hibernate's easy to integrate

    1. Add Spring 2.0 in Libraries Choose the following four jar, and configure the / WEB-INF/lib under Spring2.0 AOP Libraries Spring2.0 Core Libraries Spring2.0 Persistence Core Libraries Spring2.0 WEb Libraries At the same time, the applicationContext ...

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • jboss ejb3 Message Driven Bean

    Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • spring struts2.0 hibernate bug killer 1

    exception There is no Action mapped for namespace / and action name checkLogin. - [Unknown location] com.opensymphony.xwork2.DefaultActionProxy.prepare (DefaultActionProxy.java: 186) org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries