First, the method parameter
Action
Packagecom.pb.web.action; Public classhourseaction { PublicString Add () {System.out.println ("Perform the Add action! "); return"Success"; } PublicString Update () {SYSTEM.OUT.PRINTLN ("Perform the update operation!" "); return"Success"; } PublicString Delete () {System.out.println ("Perform the delete operation! "); return"Success"; }}
Struts.xml
<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> <!--<constant name= "Struts.enable.DynamicMethodInvocation" value= "false"/> <constant na Me= "Struts.devmode" value= "false"/> < PackageName= "Default" namespace= "/"extends= "Struts-default" > <default-action-ref name= "index"/> <global-results> <result name= "error" >/ERROR.JSP</RESULT&G T </global-results> <global-exception-mappings> <exception-mapping exception= "Java.lang.Exc Eption "result=" error "/> </global-exception-mappings> <action name=" index "> <re Sult type= "Redirectaction" > <param name= "actionname" >HelloWorld</param> <p Aram Name= "namespace" >/example</param> </result> </action> </ Package> <include file= "example.xml"/>--<!--ADD packages here--><constant name= "Struts.devmode" V Alue= "true"/>< PackageName= "Default" namespace= "/"extends= "Struts-default" ><action name= "Hourse_add"class= "Com.pb.web.action.HourseAction" method= "Add" ><result>addsuccess.jsp</result></action><action name= "Hourse_update"class= "Com.pb.web.action.HourseAction" method= "Update" ><result>updatesuccess.jsp</result></action><action name= "Hourse_del"class= "Com.pb.web.action.HourseAction" method= "Delete" ><result>deletesuccess.jsp</result></action></ Package></struts>
Default Action Configuration
<!--Default Action -<Default-action-refname= "Index" /><Actionname= "Index"><result>index.jsp</result></Action>
Second, using dynamic method invocation
Packagecom.pb.web.action; Public classuseraction { PublicString Add () {System.out.println ("Perform the Add action! "); return"Success"; } PublicString Update () {SYSTEM.OUT.PRINTLN ("Perform the update operation!" "); return"Success"; } PublicString Delete () {System.out.println ("Perform the delete operation! "); return"Success"; }}
Struts.xml
<Packageextendsclass= "Com.pb.web.action.UserAction" ><result> usersuccess.jsp</result></action>
Page
<!--invoke the specified method using an exclamation point -<formAction= "User!add"><inputtype= "Submit"value= "Add"/></form><formAction= "User!update"><inputtype= "Submit"value= "Update"/></form><formAction= "User!delete"><inputtype= "Submit"value= "Delete"/></form>
Another way of writing
<!--invoke the specified method using an exclamation point -<formAction= "User!add.action"><inputtype= "Submit"value= "Add"/></form><formAction= "User!update.action"><inputtype= "Submit"value= "Update"/></form><formAction= "User!delete.action"><inputtype= "Submit"value= "Delete"/></form>
Third, use wildcard characters to simplify configuration
Action
Packagecom.pb.web.action; Public classhourseaction { PublicString Add () {System.out.println ("Perform the Add action! "); return"Success"; } PublicString Update () {SYSTEM.OUT.PRINTLN ("Perform the update operation!" "); return"Success"; } PublicString Delete () {System.out.println ("Perform the delete operation! "); return"Success"; }}
Struts.xml
<constantname= "Struts.devmode"value= "true" />< Packagename= "Default"namespace="/"extends= "Struts-default"><Actionname= "Hourse_*"class= "Com.pb.web.action.HourseAction"Method= "{1}"><result>{1}success.jsp</result></Action></ Package>
Page
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body><formAction= "Hourse_add"><inputtype= "Submit"value= "Add"/></form><formAction= "Hourse_update"><inputtype= "Submit"value= "Update"/></form><formAction= "Hourse_delete"><inputtype= "Submit"value= "Delete"/></form></Body></HTML>
STRUTS2 (iv) Action two configuration