Struts1.2中的LookupDispatchAction配置

来源:互联网 发布:php array push 编辑:程序博客网 时间:2024/05/29 11:24

版本:eclipse 3.3,myeclipse 6.0,tomcat 6.0

在myeclipse中新建项目StrutsDemo,导入Struts1.2属性

在com.shiryu.action包中新建TestAction.java

package com.shiryu.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.LookupDispatchAction;

public class TestAction extends LookupDispatchAction {
    public ActionForward login(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        DynaActionForm registerForm = (DynaActionForm) form;// TODO Auto-generated method stub

        request.setAttribute("name", "login");
        System.out.print("login");
        return mapping.findForward("wel");
    }
   
    public ActionForward register(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        DynaActionForm registerForm = (DynaActionForm) form;// TODO Auto-generated method stub

        request.setAttribute("name", "register");
        System.out.print("reg");
        return mapping.findForward("wel");
    }

    protected Map getKeyMethodMap() {
        // TODO Auto-generated method stub
        Map map=new HashMap();
        map.put("testForm.login", "login");
        map.put("testForm.register", "register");
        return map;
    }

}

配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
</servlet>


<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>


<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

配置struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans >
    <form-bean name="testForm" type="org.apache.struts.action.DynaActionForm"/>
</form-beans>

<global-exceptions />
<global-forwards>
      <forward name="wel" path="/wel.jsp" />
      <forward name="err" path="/err.jsp" />
</global-forwards>
<action-mappings >
   
    <action
      input="/index.jsp"
      path="/test"
      type="com.shiryu.action.TestAction"
      parameter ="testForm"
      name="testForm">
    </action>

     
</action-mappings>
<message-resources parameter="com.shiryu.ApplicationResources" />
</struts-config>

新建index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
    </head>

    <body>   
    <html:form action="/test">
      <html:submit property="testForm">
         <bean:message key="testForm.login"/>
      </html:submit>
      <html:submit property="testForm">
         <bean:message key="testForm.register"/>
      </html:submit>   
    </html:form>

    </body>
</html>

新建wel.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
    <%out.print(request.getAttribute("name")); %>
</body>
</html>