上一篇:struct2 核心文件
struts2动态方法调用
method属性
- 在struts.xml文件中的配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login_add" class="action.HelloWorldAction" method="add">
<result name="add">/add.jsp</result>
</action>
<action name="login_delete" class="action.HelloWorldAction" method="delete">
<result name="delete">/delete.jsp</result>
</action>
</package>
</struts>
- 在src/action建立java类HelloWorldAction
package action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
public String add(){
return "add";
}
public String delete(){
return "delete";
}
@Override
public String execute() throws Exception{
// TODO Auto-generated method stub
System.out.println("执行Action");
return "success";
}
}
3.在webRoot下建立两个页面:
- add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
add<br>
</body>
</html>
- delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<body>
delete<br>
</body>
</html>
- 发布tomcat,输入地址
http://localhost:8080/test/login_delete.action
http://localhost:8080/test/login_add.action
检验
感叹号方式(不推荐)
在方式下实现动态加载需要加上:
<constant name="structs.enable.DynamicMethodInvocation" value="true"/>
- 修改struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="structs.enable.DynamicMethodInvocation" value="true"/>
<package name="default" extends="struts-default">
<action name="login" class="action.HelloWorldAction" >
<result name="add">/add.jsp</result>
<result name="delete">/delete.jsp</result>
</action>
</package>
</struts>
- 键入地址
http://localhost:8080/test/login!add.action
http://localhost:8080/test/login!delete.action
进行检验
通配符方式
- 修改struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login_*" class="action.HelloWorldAction" method="{1}">
<result name="add">/add.jsp</result>
<result name="delete">/delete.jsp</result>
</action>
</package>
</struts>
- 部署tomcat服务器,地址栏输入
http://localhost:8080/test/login_add.action
http://localhost:8080/test/login_delete.action
网友评论