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>