content
1. Basic structure of Struts22. Struts2 configuration file
3. Configure Action
4. Dynamic method call
5. Configure Result
https://github.com/fulq1234/mystudy/tree/master/struts
1. Struts2 basic architecture
Shortcut key ctrl+alt+H, search for StrutsPrepareAndExecuteFilter
2. Struts2 configuration file
struts.xml
1. Core configuration file, mainly responsible for managing Action
2. Usually placed in WEB-INF/classes, under this directory struts.xml can be automatically loaded
Struts.xml header code search method:
The jar package struts2-core-2.3.34.jar has struts-2.3.dtd below, and there is an example of how to write the header of struts.xml .
<struts> <constant name="" value=""/><!--常量--> <package name="" namespace="" extends=""> <action name="" class=""> <result name=""></result> </action> </package> </struts>constant element
1. Configure constants, which can change some behaviors of the Struts2 framework
2. The name attribute represents the constant name, and the value attribute represents the constant value
These default values are placed in struts2-core-2.3.34.jar, under /org/apache/struts2/default.properties
package element 1. The role of the package
: simplify maintenance and improve reusability 4.extends attribute specifies the package to be extended 5.namespace attribute defines the namespace of actions in this package, optional Unless there is a compelling reason, custom packages should always extend the struts-default package
Actions cannot have the same name in the same package .
namespace is the prefix of the access path. For example, the path of the following method action is "/user/helloworld"
<package name="default" namespace="/user" extends="struts-default"> <action name="helloworld" class="cn.HelloWorld"> <result name="success">/helloWorld.jsp</result> </action> </package>
1.struts-default.xml
Struts2 default configuration file, the
struts-default package will be automatically loaded and defined in the struts-default.xml file
2.struts-plugin.xml
Configuration files used by Struts2 plugins
Load order:
3. Configure Action
The role of Action
1. Encapsulates the unit of work
2. The place for data transfer
3. Returns the result string
method attribute
1. Default value: execute
2. Change the attribute value to implement the invocation of different methods in the Action
controller
package cn.intro.action; public class UserAction{ public String login(){ return "alogin"; } public String register(){ return "aregister"; } }
struts.xml
<package name="user" namespace="/user" extends="struts-default"> <action name="login" class="cn.intro.action.UserAction" method="login"> <result name="alogen">/login.jsp</result><!--The login.jsp file is under WebContent--> </action> <action name="register" class="cn.intro.action.UserAction" method="register"> <result name="aregister">/register.jsp</result><!--WebContent下面有register.jsp文件--> </action> </package>
The access address is as follows:
http://localhost:8080/ch01/user/register
http://localhost:8080/ch01/user/register.action
http://localhost:8080/ch01/user/login
http://localhost:8080/ch01/user/login.action
Configure the default Action
1. When no Action matches the request, the default Action will be executed2. Configure the default Action through the <default-action-ref.../> element
<struts> <package name="default" extends="struts-default"> <default-action-ref name="defaultAction"/> <action name="defaultAction"><!--Omit the class attribute, the ActionSupport class will be used. And the two "defaultAction" must be able to correspond to --> <result>error.jsp</result> </action> </package> </struts>
4. Dynamic method invocation
Dynamic method invocation
: reduce the number of Actions
using: actionName!methodName.action
Disable: set the property struts.enable.DynamicMethodInvocation to false
controller
package cn.intro.action; public class UserAction{ public String login(){ return "alogin"; } public String aa(){ return "aregister"; } }
struts.xml
<?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> <!-- Enable dynamic method invocation--> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="user" namespace="/aa" extends="struts-default"> <action name="user" class="cn.intro.action.UserAction"> <result name="alogin">/login.jsp</result> <result name="aregister">/register.jsp</result> </action> </package> </struts>
Visited url:
http://localhost:8080/ch01/aa/user!aa.action
http://localhost:8080/ch01/aa/user!login.action
Call the method user!login.action to call the login() method of UserAction
Simplify Action configuration Another form of dynamic method invocation (using wildcard *)
<action name="*User" class="cn.UserAction" method="{1}"> <result>/page/{1}_success.jsp</result> <result name="input">/page/{1}.jsp</result> </action>
explain. If * is add, {1} is add
Example:
controller
package cn.intro.action; public class UserAction{ public String add(){ return "input"; } public String dele(){ return "input"; } }
struts.xml
<?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> <!-- Enable dynamic method invocation--> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="user" namespace="/" extends="struts-default"> <action name="*User" class="cn.intro.action.UserAction" method="{1}"> <!-- <result>{1}_success.jsp</result> --> <result name="input">{1}.jsp</result> </action> </package> </struts>
Add pages add.jsp and dele.jsp to WebContent
access method
http://localhost:8080/ch01/addUser.action
http://localhost:8080/ch01/deleUser.action
5. Configure Result
Result Common result types
1.dispatcher: the default result type, the background uses RequestDispatcher to forward the request
2.redirect: the sendRedirect() used in the background redirects the request to the specified URL
3.redirectAction: mainly used to redirect to Action
<action name="*User" class="cn.UserAction" method="{1}"> <result name="input">/page/{1}.jsp</result> <result type="redirect">/page/{1}_success.jsp</result> </action>
controller
package cn.intro.action; public class UserAction{ private String id = "";//The parameters of the received page private String message = "";//Parameters returned to the page //... ...id, message's getter/setter method public String add(){ if(id.length() == 0){ this.message = "Operation failed"; return "input"; }else{ this.message = "Operation succeeded"; return "success"; } } public String dele(){ return "input"; } }
struts.xml
<?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> <!-- Enable dynamic method invocation--> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="user" namespace="/" extends="struts-default"> <action name="*User" class="cn.intro.action.UserAction" method="{1}"> <result>{1}_success.jsp</result> <result name="input" type="redirect">{1}.jsp</result> </action> </package> </struts>
add_success.jsp
<%@taglib prefix="s" uri="/struts-tags" %> <body> <s:property value="message"/> <s:form action="/addUser.action" method=""> <s:textfield name="id"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body> </html>
and add.jsp
访问http://localhost:8080/ch01/add_success.jsp
When the input box is empty, jump to the add.jsp page, and when it is worthwhile, jump to the add_success.jsp page
Global resultRealize that multiple actions in the same package share a result
<struts> <package name="default" extends="struts-default"> <global-results> <result name="error">/page/error.jsp</result> <result name="login" type="redirect">/page/login.jsp</result> </global-results> ...omitting the configuration of the action... </package> </struts>
Dynamic call (redirectAction)
struts.xml
<?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> <!-- Enable dynamic method invocation--> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="user" namespace="/" extends="struts-default"> <!-- Demonstrate dynamic results, ${nextDispose} means calling the getNextDispose() method of Action. How to get students. --> <action name="doLogin" class="cn.intro.action.UserAction" method="doLogin"> <result type="redirectAction">${nextDispose}</result> </action> <action name="manager"> <result>manager.jsp</result> </action> <action name="common"> <result>common.jsp</result> </action> </package> </struts>
login.jsp
<%@taglib prefix="s" uri="/struts-tags" %> <s:form action="doLogin.action" method="post" theme="xhtml"> <s:textfield name="name" value="xx" label="用户名"></s:textfield> <s:password name="password" label="密码"></s:password> <s:checkbox name="manager" label="Administrator login"/> <s:submit value="提交"/> </s:form>
There are two files: common.jsp, manager.jsp
UserAction.java
package cn.intro.action; public class UserAction{ private String nextDispose = ""; private boolean manager; // logic for user login public String doLogin(){ // omitted login logic if(isManager()){ nextDispose = "manager"; }else{ nextDispose = "common"; } return "success"; } //nextDispose, getter/setter method of manger }
Running effect: visit: http://localhost:8080/ch01/login.jsp
If the check box is checked, click login, the manager.jsp page will appear; if the check box is not checked, click login, the common.jsp page will appear.