码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2之动态方法调用(优点:调用同一个action中的多个方法不需要在配置文件中写多个指向相同action类的的action节点只需要一个action节点就行)

时间:2015-02-02 14:14:39      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:struts2动态方法调用   动态方法调用   

在表单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节点就行)

标签:struts2动态方法调用   动态方法调用   

原文地址:http://blog.csdn.net/u012871207/article/details/43407075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!