版本: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>
Struts1.2中的MappingDispatchAction 配置
最新推荐文章于 2025-12-03 10:04:35 发布
本文详细介绍如何使用MyEclipse创建Struts项目,并通过具体示例演示如何配置Struts框架来实现用户登录与注册功能。文章包括了核心配置文件web.xml和struts-config.xml的设置方法,以及TestAction类的具体实现细节。

2168

被折叠的 条评论
为什么被折叠?



