1.新建一个web项目并加入Struts1.x的依赖库。
引用
commons-beanutils-1.8.0.jar: 动态的获取/设值Java Bean的属性
commons-digester-1.8.jar: 基于规则的XML文档解析,主要用于XML到Java对象的映射
commons-fileupload-1.1.1.jar: struts中上传文件要用到
commons-validator-1.3.1.jar: struts的验证框架
commons-logging-1.0.4.jar:日志处理
commons-chain-1.2.jar: 处理HTTP请求处理的基础机制
struts-core-1.3.10.jar: struts的核心库
struts-extras-1.3.10.jar: struts的扩展库
oro-2.0.8.jar: 文本处理工具
antlr-2.7.2.jar:语法分析器
2.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- 以下的几个init-param都可以不用配置 -->
<init-param>
<!--
缺省情况下就是/WEB-INF/struts-config.xml,可以不用配置,只有当文件名称不是struts-config.xml或路径不在WEB-INF下时需要配置
-->
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<!-- 表示对servlet日志输出的等级 -->
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<!-- 最好留着,表示Servlet的初始化时间,在这里表示在tomcat启动的时候进行初始化 -->
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<!-- 表示servlet接收*.do的请求 -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.在WEB-INF下加入struts-config.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.yx.zzg.form.LoginActionForm" />
</form-beans>
<!-- 配置全局的转向,如果全局转向和局部转向都配有,会先找局部转向,局部转向没有找到才会去找全局转向 -->
<!--
<global-forwards>
<forward name="failure" path="/failure.jsp" />
</global-forwards>
-->
<action-mappings>
<!-- path定义action的名字,一定要以“/”开头 -->
<!-- type指定action类的完整路径 -->
<!-- name指定用的是哪个ActionForm -->
<!--
struts默认ActionForm放在session范围内,这里需要通过scope手动指定成request,所以在前台可以通过<%
LoginActionForm
laf=(LoginActionForm)request.getAttribute("loginForm");
%>拿到ActionForm
-->
<!-- 指定ActionForm中的validate是否执行,默认情况下是执行的 -->
<!--
attribute如果没有设置,在前台获取ActionForm就通过ActionForm的name来获取,如果设置了attribute就通过attribute的值来获取
-->
<action path="/login" type="com.yx.zzg.action.LoginAction"
name="loginForm" scope="request" validate="false" attribute="loginForm">
<!-- 配置转向,redirect属性默认为false,表示是请求转发,如果将其设置为true则表示重定向 -->
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/failure.jsp" />
</action>
<!--
指定访问不存在页面,当访问一个不存在的页面时会跳到unknown.jsp页面,只对.do的请求其作用。forward属性通常使用在没有具体的action实现的情况下,如一个转向
-->
<action path="/unknown" forward="/unknown.jsp" unknown="true" />
</action-mappings>
</struts-config>
4.编写ActionForm类
package com.yx.zzg.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* 登录的ActionForm.
* ActionForm是做数据收集的
* ActionForm中的属性必须和表单输入域的名称一致
* @author toshiba
*
*/
public class LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("----------LoginActionForm.reset()---------");
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("----------LoginActionForm.validate()---------");
return null;
}
}
5.编写异常类
package com.yx.zzg.exception;
public class PasswordErrorException extends RuntimeException {
}
package com.yx.zzg.exception;
public class UserNotFoundException extends RuntimeException {
}
6.编写业务类
package com.yx.zzg.service;
import com.yx.zzg.exception.PasswordErrorException;
import com.yx.zzg.exception.UserNotFoundException;
public class UserManager {
private static UserManager instance=new UserManager();
private UserManager(){
}
public static UserManager getInstance(){
return instance;
}
public void login(String username,String password){
if(!"zzg".equals(username)){
throw new UserNotFoundException();
}
if(!"zzg".equals(password)){
throw new PasswordErrorException();
}
}
}
7.编写Action类
package com.yx.zzg.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yx.zzg.exception.PasswordErrorException;
import com.yx.zzg.exception.UserNotFoundException;
import com.yx.zzg.form.LoginActionForm;
import com.yx.zzg.service.UserManager;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String errorInfo="";
LoginActionForm laf = (LoginActionForm) form;
String username = laf.getUsername();
String password = laf.getPassword();
// if ("zzg".equals(username) && "zzg".equals(password)) {
// request.setAttribute("username", username);
// return mapping.findForward("success");
// } else {
// return mapping.findForward("failure");
// }
try{
UserManager.getInstance().login(username, password);
//request.setAttribute("username", username);
return mapping.findForward("success");
}catch(UserNotFoundException fnfe){
fnfe.printStackTrace();
errorInfo="用户名无效";
}catch(PasswordErrorException pee){
pee.printStackTrace();
errorInfo="密码错误";
}
request.setAttribute("errorInof", errorInfo);
return mapping.findForward("failure");
}
}
6.登录测试页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="login.do" method="post">
用户名:
<input name="username" type="text" />
密码:
<input name="password" type="password" />
<input type="submit" value="登录" />
</form>
</body>
</html>