本文主要是介绍Struts2之动态方法调用(优点:调用同一个action中的多个方法不需要在配置文件中写多个指向相同action类的的action节点只需要一个action节点就行),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在表单action值里指定所调用的action中的哪个方法而不是借助配置文件action节点的method属性
1 UserAction类
package org.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements
ModelDriven<User/*这里需要填写Person对象*/> {
//这种实现ModelDriven的模型驱动需要的模型要实例化
private User user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String add() {
return "add";
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "minus";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
2model类
package org.model;
public class User {
private String name;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3struts.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="struts.enable.DynamicMethodInvocation" value="true" > </constant>
<package name="default" extends="struts-default">
<action name="getuser" class="org.action.UserAction">
<result name="add">/success.jsp</result>
<result name="minus">/error.jsp</result>
<interceptor-ref name="modelDriven"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
4首页
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="getuser!add" method="post"> 注:action值可以改成getuser就会调用默认方法excute
用户名:<input type="text" name="user.name"><br>
密码:<input type="text" name="user.password">
<input type="submit" value="提交">
</form>
</body>
</html>
5 转发页面error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
username:<s:property value="user.name" /><br>
password:<s:property value="user.password" />
</body>
</html>
6转发页面 success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
用户名:<s:property value="user.name" /><br>
密码:<s:property value="user.password" />
</body>
</html>
7运行截图;
(1)默认调用excute方法转发到error.jsp
2调用add方法
这篇关于Struts2之动态方法调用(优点:调用同一个action中的多个方法不需要在配置文件中写多个指向相同action类的的action节点只需要一个action节点就行)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!