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)

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 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 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 = "", 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

分类:Java 时间:2010-04-02 人气:317
blog comments powered by Disqus


  • (Transfer) JAR file package and jar commands Detailed 2010-12-19

    Often see people asking on the Internet: how to compile java programs. Exe files. Usually there are only two answers, one is to produce an executable JAR file package, then you can like. Chm run double as the document; and the other is to use JET to compi

  • Read the jar file packages at the same level directory 2009-12-31

    From the Internet to find some code modifications can be used /**----------------------------------------------- ------------------------ * getAppPath need a program to use the current Java class class attribute parameter, it can be packed off back * ...

  • Struts 2 + Hibernate 3 + Spring 2 + tiles 2 to integrate the required jar file 2009-05-15

    1) Spring 2.5 package jar required: (available through the MyEclipse / Project Capabilities / Add Spring Capabilities to add these jar files) spring-beans.jar spring-context.jar spring-core.jar commons-attributes-api.jar commons-attributes-compiler.jar co

  • When the report loads struts2 Unable to load configuration. - Bean - jar: file: / D: / Tomcat6.0/webap 2010-07-16

    The start was to use struts1 written procedures, when run directly into the IDE directly generated struts1 of the package without any problems, Today, suddenly found, struts2 have come such a long time, and also research struts1 in out, to write a simple

  • Apache httpd.conf configuration file 2011-03-22

    Apache httpd.conf configuration file, first back up what # # This is the main Apache server configuration file. It contains the # configuration directives that give the server its instructions. # See <URL:> for detai

  • JAR file jar command packet and explain 2009-09-10

    Note that the creation of the JAR file need to include a complete package and the package with the Java program structure of the corresponding directory structure, just like the above example. The Main-Class specified type, it must be complete, inclu ...

  • 15 minutes to learn to export the jar file and then install MyEclipse replace the exe executable file 2010-03-29

    This article is intended to help new to j2se packaged jar file, or do not know how a person who Zhaobi, but also to make their knowledge in the form of articles saved. First, export the jar file: If your program development has been drawing to a close, an

  • [Change] Java compressed file directory or file 2010-04-07

    package; import; import; import; import; import; import; import; / / Import ...

  • Unable to read TLD "META-INF/c.tld" from JAR file 2010-07-22

    Project start tomcat error: Serious: Servlet.service () for servlet jsp threw exception org.apache.jasper.JasperException: / index.jsp (2,0) Unable to read TLD "META-INF/c.tld" from JAR file "file: / D :/**/ WEB-INF/lib/standard -1.1.2

  • How to modify the jar file 2010-11-10

    StatSVN code under a recent statistical tools found in Chinese garbled, very helpless! By jd-gui java decompiler documents obtained, but due to some internal classes, packages, lib file is not found, it's not compiled into the project through, so

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.223 (s). 14 q(s)