struts 动态调用

1、在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="true" />
    <constant name="struts.devMode" value="true" />

    <package name="user" namespace="/" extends="struts-default">
        <action name="login" class="com.test.action.LoginAction">
            <result name="login_ok">/login_s.jsp</result>
             <result name="login_fail">/login_f.jsp</result>
        </action>
        <action name="back">
            <result>/login.jsp</result>
        </action>
    </package>

    <package name="admin" namespace="/admin" extends="struts-default">
        <action name="login" class="com.test.action.AdminAction">
           <result>/admin/index.jsp</result>
        </action>
        
        <!-- 动态调用 -->
        <action name="admin" class="com.test.action.AdminAction">
            <result>/admin/add.jsp</result>
        </action>
        <!--  
        <action name="add" class="com.test.action.AdminAction" method="add">
           <result>/admin/add.jsp</result>
        </action>
        -->
    </package>
   
    <!-- Add packages here -->

</struts>

2、调用的JSP页面代码

<%@ 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>
<a href="admin/add.do?act=add">添加</a>
<a href="admin/admin!add.do?act=add">动态添加</a>
</body>
</html>

3、action类文件

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String add()
	{
		System.out.println("添加用户");
		return SUCCESS;
	}

}


你可能感兴趣的:(struts 动态调用)