一、method参数
action
package com.pb.web.action; public class HourseAction { public String add(){ System.out.println("执行添加操作!"); return "success"; } public String update(){ System.out.println("执行更新操作!"); return "success"; } public String delete(){ System.out.println("执行删除操作!"); 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 name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> <include file="example.xml"/> --> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="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>
默认action配置
<!--默认action --> <default-action-ref name="index" /> <action name="index"> <result> index.jsp </result> </action>
二、使用动态方法调用
package com.pb.web.action; public class UserAction { public String add(){ System.out.println("执行添加操作!"); return "success"; } public String update(){ System.out.println("执行更新操作!"); return "success"; } public String delete(){ System.out.println("执行删除操作!"); return "success"; } }
struts.xml
<package name="default" namespace="/" extends="struts-default"> <action name="user" class="com.pb.web.action.UserAction"> <result> userSuccess.jsp </result> </action>
页面
<!-- 使用感叹号,调用指定方法--> <form action="user!add"> <input type="submit" value="添加"/> </form> <form action="user!update"> <input type="submit" value="更新"/> </form> <form action="user!delete"> <input type="submit" value="删除"/> </form>
另一种写法
<!-- 使用感叹号,调用指定方法--> <form action="user!add.action"> <input type="submit" value="添加"/> </form> <form action="user!update.action"> <input type="submit" value="更新"/> </form> <form action="user!delete.action"> <input type="submit" value="删除"/> </form>
三、使用通配符简化配置
action
package com.pb.web.action; public class HourseAction { public String add(){ System.out.println("执行添加操作!"); return "success"; } public String update(){ System.out.println("执行更新操作!"); return "success"; } public String delete(){ System.out.println("执行删除操作!"); return "success"; } }
struts.xml
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="hourse_*" class="com.pb.web.action.HourseAction" method="{1}"> <result> {1}success.jsp </result> </action> </package>
页面
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="hourse_add"> <input type="submit" value="添加"/> </form> <form action="hourse_update"> <input type="submit" value="更新"/> </form> <form action="hourse_delete"> <input type="submit" value="删除"/> </form> </body> </html>