项目:实现用户登录操作
用户将看到一个登录页面,要求用户输入用户名以及密码
如果用户名以及密码都是admin,提示登录成功
否则提示登录失败
1、用Eclipse创建一个Java Web应用项目
2、下载并解压Struts项目
*从Apache网站下载struts最新版
*将压缩包解压到一个目录,此目录为STRUTS_HOME目录
3、将STRUTS_HOME/lib目录下的所有.jar文件拷贝到刚创建的web项目的WebContent/WEB-INF/lib目录下
4、配置ActionServlet:
修改web项目的web.xml文件,添加如下Servlet映射配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Structs-Test</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>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name> <!--Struct的Servlet名称-->
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <!--Struct的Servelet类,ActionServlet就是struct的入口-->
<init-param>
<param-name>config</param-name> <!--Struct参数配置-->
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<!-- <init-param> -->
<!-- <param-name>config/namespace1</param-name> --> <!--Struct参数配置-->
<!-- <param-value>/WEB-INF/struts-config-namespace1.xml</param-value> -->
<!-- </init-param>-->
<!--配置命名空间:该配置下请求*.do会调用struts-config.xml,请求namespace1/*.do会调用struts-config-namespace1.xml-->
<load-on-startup>2</load-on-startup> <!--是否启动时加载-->
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name> <!--Servelet的url映射-->
<url-pattern>*.do</url-pattern> <!--所有的*.do URL-->
</servlet-mapping>
<!--这个Servlet是Struct的入口,所有已*.do结尾的URL都会被映射到Struct的servlet上,该Servlet会根据struts-config.xml里面的配置,将请求分配到指定的Action上。运行代码都在Action中实现-->
</web-app>
一个ActionForm– LoginActionForm.java
一个Action– LoginAction.java
一个struts-config.xml文件
三个页面
登录页面 – login.jsp
登录成功提示页面 – login_success.jsp
登录失败提示页面 – login_error.jsp
package com.bjsxt.strutstest;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**java会调用request.getParameter("")将值取出,然后设置到fromBean的属性上*/
public class LoginActionForm extends ActionForm {
private String username;
private String password;
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
}
package com.bjsxt.strutstest;
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;
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {//mapping代表struts-config.xml的配置,from表示封装后From对象,request和response作为请求与回应
//将ActionForm强制转换为LoginActionForm
LoginActionForm loginForm = (LoginActionForm)form;
//从LoginActionForm中提取从页面表单传递过来的参数
String username = loginForm.getUsername();
String password = loginForm.getPassword();
//根据这些参数,执行业务逻辑操作
if("admin".equals(username) && "admin".equals(password)){
request.setAttribute(username, "admin");
request.setAttribute(password, "admin");//在request中设置属性,在页面中可以接受到request.getAttribute("username")
//如果用户名和密码均为admin,则转向登录成功页面
return mapping.findForward("success");
}else{
//否则转向登录失败页面
return mapping.findForward("error");
}
}
}
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans> <!--FromBean实现类-->
<form-bean name="loginForm" type="com.bjsxt.strutstest.LoginActionForm"/>
</form-beans>
<action-mappings>
<!--path="/login"访问login.do时将调用该Action, type:Action实现类 ,name:FromBean属性类 ,scope:作用域-->
<action path="/login" type="com.bjsxt.strutstest.LoginAction" name="loginForm" scope="request"> <!--作用域-->
<!--struct将提交的表单封装成LoginActionForm对象,并且通过反射调用LoginAction的execute-->
<forward name="success" path="/login_success.jsp"/> <!--登陆成功页面-->
<forward name="error" path="/login_error.jsp"/> <!--登陆失败页面-->
</action>
</action-mappings>
<message-resources parameter="com.bjsxt.strutstest.ApplicationResources" />
</struts-config>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>请登录</title>
</head>
<body>
<form action="login.do" method="post">
请输入用户名: <input type="text" name="username"> <errors property="username"/> <br/>
请输入密码 : <input type="password" name="password"> <errors property="password"/> <br/>
<input type="submit" name="submit1" value="登录">
<!-- 点击提交表单执行 */login.do 请求,根据web.xml的配置走相应的ActionServlet -->
</form>
</body>
</html>
login_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录成功</title>
</head>
<script type="text/javascript">
var a = request.getAttribute("username");
</script>
<body>
欢迎您,您已经成功登录!您创建的第一个Struts应用程序已成功运行!!!<br>
您的姓名:${ loginForm.username}.<br>;
您的密码:${ loginForm.password}.<br>
</body>
</html>
login_error.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录失败</title>
</head>
<body>
您的登录失败了,可能原因是用户名或密码不正确,请返回重新输入
<a href="login.jsp">返回登录页面</a>
</body>
</html>
当你从浏览器输入地址:
http://localhost:8088/Struts-Test/login.jsp,Tomcat将按通常情况来处理这个JSP并返回浏览器
,将这个请求发送给相应的Servlet,在我们的应用中,Tomcat将会把这个请求发送给org.apache.struts.action.ActionServlet这个类
(请参看web.xml的配置)
然后ActionServlet根据struts-config.xml的配置信息,调用LoginAction对象去处理这个请求,在此之前,它会将页面表单的请求数据
封装到LoginActionForm对象中,并传递给LoginAction
LoginAction返回一个ActionForward对象,包含了将要转向的路径信息
ActionServlet根据这个ActionForward对象所包含的路径信息,调用相应的页面去执行响应