1 创建一个web工程
2 将Struts包引入web下的lib目录中
struts-1.3.10:http://pan.baidu.com/s/1c2IhRSs
3 创建login.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 'login.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">
-->
</head>
<body>
<form action="/StrutsLoginJ/login.do" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
4 创建ActionForm(用户表单)和Action
public class UserForm extends ActionForm {
// 定义属性【属性名应该和jsp的控件名称一致,但是方法名(getUsername)必须与jsp控件名(username)保持一致】
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
System.out.println("正在初始化username " + username);
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class LoginAction extends Action {
// 需要重新编写一个方法:execute会被自动调用,有点类似servlet中的service方法
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("hello world!");
// 把ActionForm转换成对应的UserForm对象
UserForm userform = (UserForm) form;
System.out.println(userform.getUsername());
if("123".equals(userform.getPassword())) {
return mapping.findForward("ok");
} else {
return mapping.findForward("err");
}
}
}
5 创建Struts-config.xml文件,该文件用于配置ActionForm、action,对应关系,跳转关系,一般放在WEB-INF目录下
<?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>
<!-- name 是表单名字,随意写,但是建议表单类名小写
type 用于指定表单类的全路径
-->
<form-bean name="userForm" type="com.test.forms.UserForm"></form-bean>
</form-beans>
<!-- 配置action -->
<action-mappings>
<!-- 配置具体的一个action path:表示将来访问该action的资源名称 http://localhost:8080/web应用/path?
name: 用于关联某个表单
type: 用于指定该action类的全路径
scope 表示该action对应的表单对象的生命周期 request = request.setAttribute("formName", userForm);
配置scope后,会将ActionForm对象放入request或session中
scope 默认是session
-->
<action path="/login" name="userForm" scope="request" type="com.test.actions.LoginAction">
<!-- 这里配置跳转关系
name 表示结果名称, path 转发到哪个文件
-->
<forward name="ok" path="/WEB-INF/wel.jsp"></forward>
<forward name="err" path="/WEB-INF/err.jsp" />
</action>
</action-mappings>
</struts-config>
6 编写wel.jsp和err.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'wel.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">
</head>
<body>
<h1>OK!</h1><br>
</body>
</html>
7 在web.xml中配置ActionServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>StrutsLoginJ</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- 配置Struts-config -->
<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>
</web-app>
8 使用filter配合Struts,解决中文乱码问题
开发一个过滤器,实现了javax.servlet.Filter接口的Servlet类
public class MyFilter1 extends HttpServlet implements Filter {
private String encoding;
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
arg0.setCharacterEncoding(encoding); // 设置接收编码
//arg1.setCharacterEncoding(encoding); // 设置接收编码
arg2.doFilter(arg0, arg1); // 必须
arg1.setContentType("text/html; charset=utf-8");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
encoding = arg0.getInitParameter("encoding");
}
}
web.xml中配置如下:
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.test.filter.MyFilter1</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
9 同一个项目,配置多个Struts配置文件
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<!-- 配置Struts-config -->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/Struts-config.xml, /WEB-INF/Struts-config-2.xml</param-value>
</init-param>
</servlet>
10 到数据库中验证用户是否合法(MySQL)
①在mysql中增加数据表
② 与数据库普通使用一样,然后实现UserService
Users user = new Users(userform.getPassword(), userform.getUsername());
UserService us = new UserService();
System.out.println("out " + user.getUser_name() + " " + user.getUser_pwd());
if(us.checkUser(user)) {
return mapping.findForward("ok");
} else {
return mapping.findForward("err");
}