Summary of important knowledge points of struts2 (2)

Source: Internet
Author: User
Tags xslt

Configuration of the

action's configuration

The example above shows that The action needs to be configured in Struts.xml to be used, and the action should be configured as a child element of the element, what is the function of the element? The
element can encapsulate a logically related set of action, result, intercepter, etc elements into a separate module, package can inherit other package, or be inherited by other package as a parent package, For example, "" In the previous example, HelloWorld this package inherits the Struts-default package. The
element has the following properties:
name: The name of the package. You must configure the
extends: The package to inherit, followed by the name of the inherited package. Optional
namespace: The namespace of the package. Optional
abstract: Defines a package as abstract, that is, it cannot contain the definition of an action. Optional
The name and extends in the properties above are already shown earlier, and then look at the application of the other two attributes.
1:namespace
Namespace configures the package's namespace, which cannot have an action of the same name within the same namespace, with an action of the same name within the different namespaces. Java-like package functionality, namespace can effectively prevent the conflict with the name of the action, because after configuring namespace, you need to add namespace to be the action prefix when you access the action.
If you do not configure namespace to represent the default namespace, you do not need to add the namespace prefix when you access it. As in the previous HelloWorld example, the Struts.xml configuration is as follows:
1.

Namespace= "/ch10" >
   <!-Define Global return type-->
   <global-results>
     <result name= "Global-result" >/welcome.jsp</result>
</global-results>
     <action name= "Register" class= "ch10. Register >
      <!-defines local return type-->
<result name= "Success" type= "Dispatcher" >/ch10/success.jsp</ result>
<result name= "input" >/ch10/register.jsp</result>
</action>
</ Package>

Name Type:

The result type of the chain:action chain process, which concatenates two consecutive action, completes the action through the getter method of the previous action and the corresponding setter method of the latter action.

Chart: The result type used to consolidate jfreechart.
Dispatcher: The default type, which is used to consolidate the result type of a JSP. Equivalent to a request forwarding in the servlet, the returned JSP page does not display its true URL, and is still the URL of the action.
Freemarker: The result type used to consolidate freemarker.
Httpheader: The result type used to handle special HTTP behavior.
Jasper: The result type used to consolidate jasperreport.
JSF: For consolidating JSF types.
Redirect: the type of result used for redirection. Redirects to another JSP page and displays the true URL of the page.
Redirectaction/redirect-action: The type of result that is used to redirect to another action.
Stream: Used to return a inputstream to the browser, typically for file downloads.
Tites: The result type used to consolidate tiles.
Velocity: The type of result used to integrate velocity.
XSLT: The result type used to consolidate xml/xslt, which is used to transform property information after an action has been performed.
PlainText: The result type used to display the original code for the page.

Description: The above is the built-in result type of the STRUTS2 framework, all defined in Struts-default.xml, the system will automatically load the type. Another type of result is supported by Plug-ins.

Type Property
The dispatcher type is the default result type of the STRUTS2 framework, which is equivalent to forwarding. So it has the characteristics of forwarding, for example, the target of dispatcher can only be that the URL in the same browser will not change, the caller and the callee share the same built-in objects (Request and response objects). If there is a location, the location can only be a page, cannot be an action, if you need to be an action, you can use chain to solve the problem.
The "Dispatcher" Resulttype, in Struts-default.xml, is configured as follows:

<result-type name= "Dispatcher"  
class= "Org.apache.struts2.dispatcher.ServletDispatcherResult"  
default = "true"/>  

It can be seen from the configuration that its corresponding implementation class is Servletdispatcherresult.
If using JSP as the implementation of the view technology, then this resulttype is most commonly used. In this Resulttype implementation, the forward method of the Javax.servlet.RequestDispatcher class is invoked, which means that it is equivalent to a repackaging of requestdispatcher.
Then in the servlet use RequestDispatcher to carry on the page to jump the characteristic, also naturally is "dispatcher" this resulttype inherits down. So what are the characteristics of the requestdispatcher in the servlet?
That is, the page jump through RequestDispatcher will remain the same request object. What good is that? Because it is the same object, it means that there is the same data, and the Request object contains many data, in the servlet's request object, typically has the following data:
The parameter area (parameter) is the data that the user fills out and submits on the page
Head area, which is automatically added to the request packet data when the browser makes the request
attribute, a value stored by the developer in the property area, usually accessed through the Request.setattribute method, the Request.getattribute method
Cookie area, where the browser automatically passes the relevant cookie data to the server when the request is made
For the use of dispatcher, in addition to the JSP can be configured, you can configure other Web resources, such as other servlet.
If you have the following configuration in Web.xml:
1.
2. Login
3. servlet. Loginservlet
4.
5.
6. Login
7./login
8.
Then you can configure the following in Struts.xml:
/login
Note, however, that if this Web resource is another action, it cannot be configured that way, and you need to use another Struts2 name called "Chain" Resulttype.
With the resulttype of dispatcher, you cannot access Web resources in other Web applications. Of course, this feature is determined by the forward method of the Javax.servlet.RequestDispatcher class.
Redirect is equivalent to redirection, so not only can you redirect other JSP pages for the same Web application, but you can also direct to other external resources. URLs are also changes to the page address after redirection. Built-in objects are not shared between the caller and the callee. Such as:



/web-inf/page/hello.jsp?msg= Messagetrue in the above configuration file, the expression {message} True is used in the redirected address in the above configuration file, the expression {message} is used in the redirected address, When the program is run, it is replaced with a specific value that is determined by the message property of the action class. If the message attribute in the action is not defined, the value of the expression ${message} is null.
Redirectactin/redirect-action is used to redirect to another action. Such as:

HelloWorld
/test

Chain is used to concatenate two consecutive execution action, such as:

1.  package action;  
2.    
3.  Import com.opensymphony.xwork2.ActionInvocation;  
4.  import Com.opensymphony.xwork2.interceptor.PreResultListener;  
5.    
6.  public class Mypreresult implements preresultlistener{    
7.      public void Beforeresult (actioninvocation actioninvocation, String result) {  
8.          System.out.println ("Now deal with the function before result execution, result=" +result);  
9.      }  

There is a Actioninvocation object in this class that encapsulates what the action needs to run, including data, proxy objects, and so on, and can even execute the action.
Then, in the Execute method of the action, register the Listener object, as shown in the following example:

1. Package cn.javass.action.action;  
2. Import Com.opensymphony.xwork2.ActionContext;  
3. Import Com.opensymphony.xwork2.ActionSupport;  
4. Import Com.opensymphony.xwork2.interceptor.PreResultListener;      5. Public class Helloworldaction extends Actionsupport {6.  
Private String account;  
7. Private String password;  
8. Private String Submitflag;     9.10.         Public String Execute () throws Exception {11.  
This.businessexecute ();       
Preresultlistener PR = new Mypreresult ();  
Actioncontext.getcontext (). Getactioninvocation (). Addpreresultlistener (PR); return this.  
SUCCESS;     15.} 16.      /** 17.      * Example method, representing a method that can perform business logic processing, 18.     * * 19.         public void Businessexecute () {20.  
System.out.println ("The user input parameter is = = = =" + "account=" +account+ ", password=" +password+ ", submitflag=" +submitflag ");     21.} 22. attribute corresponding to the Getter/setter method, omitted 23.  Struts.xml Nothing changes, is the simplest configuration, as follows: 1. <?xml version= "1.0"encoding= "UTF-8"?> 2. <!      DOCTYPE struts public 3.      "-//apache Software foundation//dtd Struts Configuration 2.0//en" 4.    
"Http://struts.apache.org/dtds/struts-2.0.dtd" > 5.      6. <struts> 7.      <constant name= "Struts.devmode" value= "true"/> 8.      <constant name= "Struts.locale" value= "Zh_cn"/> 9.   
<constant name= "struts.i18n.encoding" value= "Utf-8"/> 10.         <package name= "HelloWorld" extends= "Struts-default" > 12. <action name= "Helloworldaction" class= "action.             Helloworldaction "> 13.         <result>/s2impl/welcome.jsp</result> 14.     </action> 15. </package> 16.  </struts>

To see that Preresult did run before result, you can simply output a word in welcome.jsp, as follows:

1.  <%@ page language= "java" contenttype= "text/html; Charset=utf-8"  
2.      pageencoding= "Utf-8"%>  
3.  

Note: Some knowledge points is to see the summary of others, but do not know the specific from whose hand, again can not indicate the source, but also hope that the original author Haihan, if necessary, please tell.

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.