1. Various default values in Action configuration
<package name="csdn" namespace="/test" extends="struts-default">
<action name="helloworld" method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
1>If no class is specified for action, the default is ActionSupport.
2>If no method is specified for the action, the execute() method in the action will be executed by default.
3>If the name attribute of result is not specified, the default value is success.
2. Various forwarding types of result in Action
<action name="helloworld">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
The result configuration is similar to forward in struts1, but struts2 provides multiple result types. Commonly used types are: dispatcher (default value), redirect, redirectAction, plainText.
The following is an example of the redirectAction result type, if the redirected action is in the same package:
<result type="redirectAction">helloworld</result>
If the redirected action is in another namespace:
<result type="redirectAction">
<param name="actionName">helloworld</param>
<param name="namespace">/test</param>
</result>
plaintext: displays the original file content. For example, when we need to display the jsp file source code as it is, we can use this type.
<result name="source" type="plainText ">
<param name="location">/xxx.jsp</param>
<param name="charSet">UTF-8</param><!-- Specify the encoding for reading files -->
</result>
In the result, you can also use the ${attribute name} expression to access the attributes in the action. The attribute names in the expression correspond to the attributes in the action. as follows:
<result type="redirect">view.jsp?id=${id}</result>
3. Multiple Actions share one view - global result configuration
When the same view is used in multiple actions, we should define the result as a global view. Global forward is provided in struts1, and similar functions are also provided in struts2:
<package....>
<global-results>
<result name="message">/message.jsp</result>
</global-results>
</package>
4. Inject values into Action properties
Struts2 provides dependency injection function for the properties in Action. In the struts2 configuration file, we can easily inject values into the properties in Action. Note: Properties must provide setter methods.
public class HelloWorldAction{
private String savePath;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
...
}
<package name="csdn" namespace="/test" extends="struts-default">
<action name="helloworld" >
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
The above injects "/images" into the action's savePath attribute through the <param> node.
5. Specify the request suffix that needs to be processed by Struts 2
Previously we used the .action suffix by default to access Action. In fact, the default suffix can be modified through the constant "struts.action.extension". For example, we can configure Struts 2 to only process request paths with .do as the suffix:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value="do"/>
</struts>
If the user needs to specify multiple request suffixes, separate the multiple suffixes with commas (,). like:
<constant name="struts.action.extension" value="do,go"/>
6. Describe the definition of constants in detail
Constants can be configured in struts.xml or struts.properties. It is recommended to configure them in struts.xml. The two configuration methods are as follows:
Configure constants in the struts.xml file
<struts>
<constant name="struts.action.extension" value="do"/>
</struts>
Configure constants in struts.properties
struts.action.extension=do
Because constants can be defined in multiple configuration files below, we need to understand the search order in which struts2 loads constants:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
If the same constant is configured in multiple files, the constant value configured in the latter file will overwrite the constant value configured in the previous file.
7. Introduction to commonly used constants
<!-- Specify the default encoding set, which acts on the setCharacterEncoding method of HttpServletRequest and the output of freemarker and velocity -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- This attribute specifies the request suffix that needs to be processed by Struts 2. The default value of this attribute is action, that is, all requests matching *.action are processed by Struts2.
If the user needs to specify multiple request suffixes, separate the multiple suffixes with commas (,). -->
<constant name="struts.action.extension" value="do"/>
<!-- Set whether the browser caches static content. The default value is true (used in production environment). It is best to turn it off during the development stage -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- When the struts configuration file is modified, whether the system will automatically reload the file, the default value is false (used in production environment), it is best to turn it on during the development stage -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- Used in development mode, so that more detailed error messages can be printed out -->
<constant name="struts.devMode" value="true" />
<!--Default view theme-->
<constant name="struts.ui.theme" value="simple" />
<! When integrating with spring, specify that spring is responsible for the creation of the action object -->
<constant name="struts.objectFactory" value="spring" />
<!This property sets whether Struts 2 supports dynamic method calling. The default value of this property is true. If you need to turn off dynamic method calling, you can set this property to false. -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!--Size limit for uploaded files-->
<constant name="struts.multipart.maxSize" value="10701096"/>
8. Specify multiple struts configuration files for the application
In most applications, as the application scale increases, the number of Actions in the system will also increase significantly, causing the struts.xml configuration file to become very bloated. In order to avoid the struts.xml file being too large and bloated and improve the readability of the struts.xml file, we can decompose a struts.xml configuration file into multiple configuration files, and then include other configuration files in the struts.xml file. The following struts.xml specifies multiple configuration files through the <include> element:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-user.xml"/>
<include file="struts-order.xml"/>
</struts>
In this way, we can add Struts 2 Action to multiple configuration files by module.
9. Dynamic method call
If there are multiple methods in the Action, we can use !+method name to call the specified method. as follows:
public class HelloWorldAction{
private String message;
....
public String execute() throws Exception{
this.message = "My first struts2 application";
return "success";
}
public String other() throws Exception{
this.message = "Second method";
return "success";
}
}
Assume that the URL path to access the above action is: /struts/test/helloworld.action
To access the action's other() method, we can call it like this:
/struts/test/helloworld!other.action
If we do not want to use dynamic method invocation, we can turn off dynamic method invocation through the constant struts.enable.DynamicMethodInvocation.
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
10. Use wildcards to define actions
<package name="csdn" namespace="/test" extends="struts-default">
<action name="helloworld_*" method="{1}">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
public class HelloWorldAction{
private String message;
....
public String execute() throws Exception{
this.message = "My first struts2 application";
return "success";
}
public String other() throws Exception{
this.message = "Second method";
return "success";
}
}
To access the other() method, you can access it through a URL like this: /test/helloworld_other.action
11. Receive request parameters
Use basic types to receive request parameters (get/post)
Define an attribute with the same name as the request parameter in the Action class, and struts2 can automatically receive the request parameter and assign it to the attribute with the same name.
Request path: http://localhost:8080/test/view.action?id=78
public class ProductAction {
private Integer id;
public void setId(Integer id) {//struts2 uses reflection technology to call the setter method of the property with the same name as the request parameter to obtain the request parameter value
this.id = id;
}
public Integer getId() {return id;}
}
Use composite type to receive request parameters request path: http://localhost:8080/test/view.action?product.id=78
public class ProductAction {
private Product product;
public void setProduct(Product product) { this.product = product; }
public Product getProduct() {return product;}
}
Struts2 first creates the product object by calling the default constructor of Product through reflection technology, and then calls the setter method of the property with the same name as the request parameter in product through reflection technology to obtain the request parameter value.
12. The meaning of type conversion
For a smart MVC framework, it is inevitable to implement type conversion. Because the request parameters of B/S (browser/server) structure applications are sent to the server through the browser, these parameters cannot have rich data types. , so data type conversion must be completed on the server side
The MVC framework is a presentation layer solution and should provide type conversion support. Struts2 provides very powerful type conversion support.
13. Processing of presentation layer data
For web applications, the presentation layer is mainly used to interact with users, including collecting user input data and presenting the status of the server to users. Therefore, the data flow in the presentation layer mainly has two directions: input data and output data.
For input data: you need to complete the conversion from string data to multiple types of data. The program usually cannot be completed automatically and needs to be converted manually in the code.
For output data: Both java and jsp support direct output of multiple data types.
Another data processing in the presentation layer is: data verification, which is divided into client verification and server-side verification. This will be explained in detail later.
14. Type conversion
HTTP parameters are all string types. The saved data may be string, number, Boolean, date and time, etc. or JavaBean type. Manual type conversion, such as converting a string to a date, by: obtaining the string through the request.getParameter method; checking whether it is empty; converting the string into a Date object through the DateFormat.parse method
15. Struts2 type conversion
Struts2 built-in type conversion
String and boolean complete the conversion between string and Boolean values
String and char conversion between ordinary strings and characters
String, int, and Integer complete the conversion between strings and integers.
String and Long complete the conversion between string and long integer values
String, double, and Double complete the conversion of strings and double-precision floating point values.
String and Float complete the conversion between strings and single-precision floating point
String and Date complete the conversion between string and date types. The date format uses the SHORT format of the Locale where the user requests the format.
String and array In the default case, the array element is a string. If the user defines a type converter, it can also be other composite data types.
String and Map and List
16. Struts type conversion API
The type converter of Struts2 is actually implemented based on OGNL. There is an ognl.TypeConverter interface in the OGNL project. This interface is the interface that must be implemented to implement the type converter. The interface is defined as follows:
public interface TypeConverter {
public Object convertValue(Map arg0, Object arg1, Member arg2, String arg3,
Object arg4, Class arg5) {
return null;
}
To implement a type converter, you must implement the TypeConverter above. However, the methods in the above interface are too complicated, so the OGNL project also provides an implementation class of this interface: ognl.DefaultTypeConverter. You can implement your own type converter by inheriting this class. The class is defined as follows :
public class DefaultTypeConverter extends Object implements TypeConverter{
public Object convertValue(Map<String,Object> context, Object value, Class toType) {
}
……//Other methods
}
The role of the ConvertValue method is to complete type conversion, but this type conversion is bidirectional. When a string needs to be converted into an object instance, this method is used. When an object instance is converted into a string, this method is also used. This conversion is done through the toType parameter type which is the target type that needs to be converted. Therefore, the conversion direction can be determined based on the toType parameter.
ConvertValue method parameters and return meaning The first parameter: context is the context of the type conversion environment. The second parameter: value is the parameter that needs to be converted. The value of the value parameter is also different depending on the conversion direction.
The third parameter: toType is the converted target type. The return value of this method is the value after type conversion. The type of the value also changes with the direction of the conversion. It can be seen that the converted convertValue method accepts the value that needs to be converted, the target type that needs to be converted is a parameter, and then returns the converted target value.
Why is Value a string array?
For the DefaultTypeConverter converter, it must take into account the most common situation, so it treats all request parameters as string arrays instead of strings. Equivalent to the parameter values obtained by getParameterValues()