web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</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>
sturts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources />
<form-beans>
<!-- FormBean可以配置为静态FormBean与动态FormBean
+ 静态FormBean
|- ActionForm
|- ValidatorForm
|- ValidatorActionForm
+ 动态FormBean
|- DynaActionForm
|- DynaValidatorForm
|- DynaValidatorActionForm
-->
<form-bean name="user" type="com.wj.pojo.User"></form-bean>
<form-bean name="productForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="productName" type="java.lang.String"></form-property>
<form-property name="productPrice" type="java.lang.Double"></form-property>
<form-property name="productDate" type="java.util.Date"></form-property>
<form-property name="discription" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<!-- <action
path="/地址" //拦截的地址
type="" //Action 具体地址
parameter="" //表示访问Action方法的标识,如:method=deleteUser 将访问deleteUser方法
input="" //指定验证错误返回的页面
validate="true" //是否启动输入的效验
scope="request"
>
<forward
name="index" //在Action中,ActionMappding.findForward("index"),用户查找调转
path="/index.jsp" //跳转页面的具体地址
/>
</action>
-->
<action path="/login"
input="/index.jsp"
validate="true"
scope="request"
parameter="method"
type="com.wj.action.LoginAction"
>
<forward name="tag" path="/tag.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.wj.struts.ApplicationResources" />
</struts-config>
Action
package com.wj.action;
import java.util.ArrayList;
import java.util.List;
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.actions.DispatchAction;
import com.wj.pojo.User;
public class LoginAction extends DispatchAction{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println(request.getParameter("userName")+" 开始验证登录.");
System.out.println("密码为[ "+request.getParameter("password")+" ]");
System.out.println("LoginAction execute()");
return super.execute(mapping,form,request,response);
}
public ActionForward checkLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("开始检查用户是输入是否正确.");
return mapping.findForward("error");
}
public ActionForward toTag(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception{
List userList = new ArrayList();
userList.add("张三");
userList.add("李四");
userList.add("王五");
User user = new User();
user.setAge(33);
user.setName("赵六");
userList.add(user);
//设置 Bean到 request环境中
request.setAttribute("userList", userList);
//跳转到 标签库页面
return mapping.findForward("tag");
}
}
index.jsp
<%@ 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" mce_href="styles.css">
-->
</head>
<body>
<form action="login.do?method=checkLogin" method="post">
<input name="userName"/>
<input name="password" type="password"/>
<input type="submit" value="登录"/>
</form>
<hr/>
<a href="login.do?method=toTag" mce_href="login.do?method=toTag"><h2>Struts1.x 标签库</h2></a>
</body>
</html>