This article mainly includes: setting struts2 matching extension, using wildcard character, value stack, declarative exception, and label.
Set the extension name:
By default, the. Action and no extension are processed. Can be set:
<constant name= "struts.action.extension" value= "Action,do," ></constant>//to. Action,.do and no interception
Wildcard characters:
To simplify the configuration of the action, you can use a wildcard character, roughly as follows:
<action name= "user_*" class= "jsky.action.ActionTestAction" method= "{1}" > <result name= "Success" >/pages/{1}.jsp</result> <result name= "error" >/pages/error2.jsp</result> </ Action>
Rule: Precedence matches no wildcard, if multiple wildcard action, in order {1}{2} first, two strings
Value stack:
Stack of action, divided into object stacks and map stacks
object stack: access mode: object.propertyname; object[' PropertyName ']; object[" PropertyName "]
and nbsp objects can be referenced by a zero-based subscript. The top object in the Objectstack can be referenced by [0], ,
The object under it can be referenced using [1], starting from [0] objects to search for matching objects
name: <s:property value= "[0].name"/>
Pass: <s:property value= "Pass"/>
Map stack: The inside holds various Map:requestmap, Sessionmap, Applicationmap, Parametersmap, attr
Access method: #object. PropertyName; #object [' PropertyName ']; #object ["PropertyName"]
Session:<s:property value= "#session. SessionKey2"/>
Declarative Exceptions:
Specifies the page to go when the action is out of an exception, with the following approximate usage:
result specifies the page to go after the exception, exception refers to the type of exception that occurred
<action name= "Actiontest2" class= "jsky.action.ActionTestAction" method= "Log" > < exception-mapping result= "error" exception= "Java.lang.ArithmeticException" ></exception-mapping> <result name= "Success" >/pages/show2.jsp</result> <result name= "error" >/pages/error2.jsp </result> </action>
Label:
Struts2 with a special number of labels, here simply mention:
pass:<s:property value= "Pass"/> Object
Session:<s:property value= "#session. SessionKey2"/><br>map
Finally, a registered function to solidify these tags, the effect is as follows:
The core code is as follows:
input.jsp
<body> <s:form action= "Regis_save" > <s:textfield name= "name" label= "Name" > </s: textfield> <s:password name= "password" label= "password" ></s:password> <s:radio name= " Gender "list=" #{' 1 ': ' Male ', ' 0 ': ' femal '} "label=" Gender "></s:radio> <s:select name=" department " List= "#request. Department" listkey= "DepartmentID" listvalue= "Departmentname" label= "department" ></s:select > <s:checkboxlist name= "role" list= "#request. Role" listkey= "Roleid" listvalue= "RoleName" label= "role" > </s:checkboxlist> <s:submit value= "Submit" ></s:submit> </s:form> </body >
WELCOME.JSP:
<body>name: ${name}<br>pass:${password}<br>gender:${gender}<br>department: ${department} <br>role:${role}</body>
The main action:
Public String input () {request.put ("department", Dao.getdept ()), Request.put ("Role", Dao.getrole ()); return "Input";} Public String Save () {return "Success";} Private map<string, object> request; @Overridepublic void Setrequest (map<string, object> arg0) {//TODO auto-generated method Stub this.request=arg0;}
STRUTS2 basic Carding (II.)