Struts2. X Experience 3--struts2 Introductory Knowledge 2

Source: Internet
Author: User

1. Mvc and Servlet

the benefits of a servlet

is the foundation of MVC, and other frameworks such as Struts1,struts2,webwork are developed from the servlet base. so mastering the servlet is the key to mastering MVC. The servlet leaks the lowest API to the programmer, giving the programmer a clearer idea of the various features of MVC. The programmer can encapsulate the servlet. STRUTS2 is the result of encapsulating from the servlet.

The disadvantage of the servlet

Each servlet should be configured in the Web.xml. If you have more than one servlet, it can cause web.xml content to be too numerous. such a structure is not conducive to group development . In the servlet, the Doget method and the Dopost method have HttpServletRequest and HttpServletResponse parameters. These two parameters are related to the container, and if you want to make unit tests in the servlet, you must initialize both of these parameters. If there are many methods in a servlet, they must be decomposed into each method in the form of passing arguments.

2 refactoring Servlet

For the features above the servlet, we can refactor the servlet to make it easier to develop. Easier and more suitable for teamwork.

The goal of refactoring:

1, only write a serlvet or filter, we choose the filter here.

2, no longer write any servlet, so the code written in the Web.xml is very little.

3), the original need to write Serlvet, now rewrite action.

4), in the action of the HttpServletRequest parameters and HttpServletResponse parameters passed past.

5, in the filter through the Java Reflection Mechanism invoke action.

3 Struts2 Introduction

1), Struts2 is an open source framework invented by the Apache organization. Is the second generation of struts products.

2), STRUTS2 is a new framework for the integration of struts and webwork.

3, STRUTS2 configuration file organization is more reasonable, is a good choice for enterprise development.

4, Struts2 interceptors inject a whole new concept into the MVC framework.


4. The difference between filter and interceptor:

* same point: all the interception

* Different points:

* Scope of Action:

Filter: The scope of use is Java EE category, any Web project can use filters;

Interceptor: The scope of use is struts2 category, inseparable from the STRUTS2 framework

* Completed functions:

Filter: Intercepting request resource; Interceptor: Complete All other functions

* Order of execution:

Filter--> Interceptor

5. Loading Struts.xml Process

Description:

1), loading three profiles at startup

Struts-default.xml, Struts-plugin.xml, Struts.xml

2), if the three files have the same item, the back covers the front.

3), struts.xml files must be placed in the SRC to find

6. Introduce additional configuration files in Struts.xml

<includefile= "Csdn/action/include/struts-include.xml" >

</include>

This allows the Struts-include.xml file to be loaded when the Struts.xml file is loaded.

Various forwarding types of result in 7.Action

The result configuration is similar to the forward in struts1, but there are several types of results available in struts2, commonly used types: Dispatcher (default), redirect, Redirectaction, plaintext.

1, each action method returns a string value, and what value is determined by what struts once requests to return.

2, in the configuration file, the configuration of each action element must have a result element, each result corresponds to the return value of an action.

3), result has two attributes:

Name: The result is the same as the return value in the action, and the default value is success;

Type: Response result types, the default value is dispatcher.

4), you can see that results and result sets of 10 types; The default type is servletdispatcherresult; The result type can be any of these 10 result types.

A, dispatcher in a configuration file, there are two ways to do this:

The first method of writing:

<resultname= "Success" >/resulttype/successDispatcher.jsp</result>

The second way:

<resultname= "Success" >

<paramname= "Location" >/resulttype/successDispatcher.jsp</param>

</result>

B, redirect belong to redirection. If the redirect type is used, the value in the Reuqest scope cannot be passed to the foreground.

C, redirectaction type

(1), Description

Redirect the result type to action

Can accept two kinds of parameters

A) Actionname:action's name

b) Namespace: namespaces

(2), examples

The first way:

<result name= "Success" type= "Redirectaction" >resulttype/redirectactionAction.action</result>

The second way:

<result name= "Success" type= "Redirectaction" >

<!--

ActionName:

The path of the requested action

Namespace

If it is not written, the default is the path of the requested action, and if it is written, the path will be assigned a new value.

-->

<param name= "ActionName" >

Resulttype/redirectactionaction.action

</param>

</result>

 


The following is an example of the redirectaction result type, if the redirected action

Under the same package:

<resulttype= "Redirectaction" >helloworld</result>

If the redirected action is under another namespace:

<resulttype= "Redirectaction" >

<paramname= "ActionName" >helloworld</param>

<param name= "namespace" >/test</param>

</result>

PlainText: Display the original file content, for example: when we need to display the JSP file source code, we can use this type.

<resultname= "source" type= "plaintext" >

<paramname= "Location" >/xxx.jsp</param>

<paramname= "CharSet" >UTF-8</param><!--Specifies the encoding to read the file-->

</result>

In result, you can also use the ${property name} expression to access the properties in the action, and the property names in the expression correspond to the properties in the action. As follows:

<resulttype= "redirect" >view.jsp?id=${id}</result>

8. Multiple action sharing a view--global result configuration

When the same view is used in multiple action, we should define result as a global view. Similar features are provided in the global forward,struts2 in Struts1:

<package....>

<global-results>

<result name= "Message" >/message.jsp</result>

</global-results>

</package>

9. Inject values for the property of the action

STRUTS2 provides dependency injection for attributes in the action, and in the Struts2 configuration file, we can easily inject values into the properties of the action. Note: The property must provide a setter method.

Publicclass helloworldaction{

Private String Savepath;

Public String Getsavepath () {

return savepath;

}

public void Setsavepath (String savepath) {

This.savepath = Savepath;

}

......

}

<packagename= "Csdn" namespace= "/test" extends= "Struts-default" >

<action name= "HelloWorld" class= "Cn.csdn.action.HelloWorldAction" >

<param name= "Savepath" >/images</param>

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

</package>

"/images" is injected above through the Savepath property of the <param> node action.

Dependency Injection: Control reversal The IOC is itself not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects are handled by external containers. The direction of control is shifted to the outer container, and the transfer of control is the so-called reversal. The external container dynamically injects dependent objects into the component during run time.

A lot of injection methods: such as Set method injection

10. Action Prototype mode

1), review servlet

In the servlet, a servlet has only one object, that is, the servlet is a single instance pattern. If you write a collection in the servlet attribute, consider thread-safe issues.

2), action multiple case mode

But in the STRUTS2 framework, this is not the case, that is, the action in Struts2, which, if accessed once, instantiates an object. There is no thread-safety problem. This is also a good

for the STRUTS2 framework.

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.