Action执行的时候并不一定要执行execute方法
可以在配置文件中配置Action的时候用method=来指定执行哪个方法也可以在url地址中动态指定(动态方法调用DMI)(推荐)
前者会产生太多的action,所以不推荐使用
Struts.xml:
复制代码
UserAction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<?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.devMode" value="true" /> <!-- 开启动态方法调用 action ! method方式 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <!-- 配置默认的访问路径 --> <action name="userAdd" class="com.smile.struts2.front.action.UserAction" method="add"> <result name="success"> /user_add_success.jsp </result> </action> <action name="user" class="com.smile.struts2.front.action.UserAction"> <result name="success"> /user_add_success.jsp </result> </action> </package> </struts>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12package com.smile.struts2.front.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { // public String execute(){ // return "path"; // } public String add(){ return SUCCESS; } }
index.jsp:
复制代码
user_add_success.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% String context = request.getContextPath(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> Action执行的时候并不一定要执行execute方法<br /> 可以在配置文件中配置Action的时候用method=来指定执行哪个方法 也可以在url地址中动态指定(动态方法调用DMI)(推荐)<br /> <a href="<%=context %>/user/userAdd">添加用户</a> <br /> <a href="<%=context %>/user/user!add">添加用户</a> <br /> 前者会产生太多的action,所以不推荐使用 </body> </html>
复制代码
这样在index.jsp上点击添加用户按钮时,都经过了UserAction的处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14<?xml version="1.0" encoding="GB18030" ?> <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>Insert title here</title> </head> <body> User Add Success! </body> </html>
最后
以上就是淡然棒棒糖最近收集整理的关于Struts2学习笔记——ActionMethod的全部内容,更多相关Struts2学习笔记——ActionMethod内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复