Struts1.2中的MappingDispatchAction 配置

本文详细介绍如何使用MyEclipse创建Struts1.2项目,并实现登录与注册功能。通过配置web.xml、struts-config.xml及编写TestAction类,演示了Struts框架的基本操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本: 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.MappingDispatchAction;

public class TestAction extends MappingDispatchAction {
    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");
    }

}

配置 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
      path="/login"
      type="com.shiryu.action.TestAction"
      input="/index.jsp"
      parameter="login">
    </action>
   
    <action
      path="/register"
      type="com.shiryu.action.TestAction"
      input="/index.jsp"
      parameter="register">
    </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>
    <form name="testForm">
      <input type="submit" value="login" onClick="document.testForm.action='login.do'"/>
      <input type="submit" value="reg" onClick="document.testForm.action='register.do'"/>
    </form>
    </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值