Struts2: Core configuration file
Package labels
- 1:Similar to code package, different
action
And you want to configureaction
, must be written firstpackage
The label,package
In order to configureaction
. - 2:
package
Tag attributes.- (1)
name
Properties:name
Attribute values have nothing to do with the function itself,- You can write more than one configuration file
package
The label,name
Attribute values cannot be the same.
- (2)
extends
Properties:- Property values are fixed
extends="struts-default"
. - I wrote this property in
package
The class inside hasaction
Function.
- Property values are fixed
- (3)
namespace
Properties:namespace
Attribute values andaction
Inside the labelname
Attribute values constitute an inter-visit path.
- (1)
The action tags.
- 1:
action
The tagaction
Visit path between - 2:
action
Tag attributes- (1)
name
Properties:namespace
Attribute values andaction
Inside the labelname
Attribute values constitute an inter-visit path.- in
package
Write multiple tags insideaction
Tag, butaction
thename
Attribute values cannot be the same
- (2)
class
Properties:action
The full path
- (3)
method
Properties:- For example, in
action
The method executed by default insideexecute
Method, but inaction
There are other methods in there. - let
action
Inside more than one method is executed, usingmethod
Configure.
- For example, in
- (1)
The result tag.
- 1:According to the
action
Method return value, configured to a different path inside. - 2:
result
Tag attributes.- (1)
name
Properties:- Same as the method return value.
- (2)The type attribute:
- Configure how to go to the path (forward or redirect).
type
Attribute default value to perform the forwarding operation.
- (1)
Source:
- src:
struts.xml
<package name="hellodemo" extends="struts-default" namespace="/">
<! -- name: access name -->
<action name="hello" class="com.yap.action.HelloAction">
<! -- Configure method return value to page -->
<result name="ok">/hello.jsp</result>
</action>
</package>
Copy the code
Change the default Struts2 constant value
- 1:Commonly used way
- in
struts.xml
To configure.<constant name="struts.i18n.encoding" value="UTF-8"/>
- in
- 2:There are two other ways.
- in
src
The following to createstruts.properties
modified - in
web.xml
Configure.
- in
Describes the most commonly used constants
- struts.i18n.encoding = UTF-8
- 1:The form submits data to
action
Inside, inaction
You can get form submission data, - 2:Form submission data in Chinese, there are garbled problems, solve:
post
Commit directly to set the encodingget
Commit for encoding conversion
- 3:If the
action
Get the form passpost
Methods to submit Chinese, Chinese garbled between the problem to help solve, do not need to deal with their own problems.
Modular development
- 1:Write the configuration file separately and add the configuration file to the core configuration file
<include file=" path "/>
: Introduces XML paths.
How actions are written
- 1:
action
There are three ways to write- Create a normal class that inherits no classes and implements no interfaces
- Create classes to implement interfaces
Action
- Create class, inherit class
ActionSupporte
Methods to access action (emphasis)
- 1:There are three ways to do this
- (1) Use the method property of the action tag to write the method of the action to execute.
- (2) Use the wildcard method.
- (3) Dynamic inter-visit implementation (no).
- 2:Presentation error
- (1)This error occurs if the Action method returns a value but is not configured in the configuration file.
No result defined for action cn.itcast.action.HelloAction and result ok
- (2) The method in the action must return a value. If it does return a value, the type must be String
- (3)Action methods can have no return value. If there is no return value, the result tag does not need to be configured
- Write the method return value as void.
- Let’s return the value, return NONE.
- (1)This error occurs if the Action method returns a value but is not configured in the configuration file.
<! -- Use the action tag method attribute --> <! Create action, create multiple methods, the drawback is that each action method needs to be created. -->public class BookAction extends ActionSupport {
/ / add
public String add(a) {
System.out.println("add...........");
return NONE;
}
/ / modify
public String update(a) {
System.out.println("update.........");
returnNONE; }}Copy the code
- xml:
struts.xml
<! Configure action method access -->
<package name="methoddemo" extends="struts-default" namespace="/">
<! Write the name of the method in the action to be executed -->
<action name="addAction" class="com.yap.method.BookAction" method="add"></action>
<! Update method -->
<action name="updateAction" class="com.yap.method.BookAction" method="update"></action>
</package>
Copy the code
- psm:
http://localhost/Struts2_demo/updateAction.action
- psm:
http://localhost/Struts2_demo/addAction.action
Use wildcards
- 1: 在
action
The label insidename
Properties,name
Write symbols inside property values*
The asterisk - xml:
struts.xml
<! -- Wildcard implementation -->
<package name="methoddemo" extends="struts-default" namespace="/">
<! Add (2) update (action) update (book_update); Use book_* to match the method property, which is equivalent to update the above two paths.
<action name="book_*" class="com.yap.method.BookAction" method="{1}"></action>
</package>
Copy the code
- psm:
http://localhost/Struts2_demo/book_update.action
- psm:
http://localhost/Struts2_demo/book_add.action