Struts + JSP 实现用户登录(一)

strut2实现用户登录流程图

在这里插入图片描述

项目结构

在这里插入图片描述

需要的jar 包

commons-fileupload-1.3.2.jar 文件上传下载组件包
commons-io-2.4.jar 输入输出
commons-lang3-3.4.jar 数据类型工具。可以看成java.lang扩展
commons-logging-1.1.3.jar 日志管理组件依赖包
freemarker-2.3.23.jar 标签模板使用类库
javassist-3.20.0-GA.jar javaScript 字节码解释器
log4j-api-2.7.jar 显示程序运行日志
mysql-connector-java-5.1.42-bin.jar 数据库连接
ognl-3.1.12.jar 使用一种表达式语言类库
struts2-core-2.5.8.jar struts核心类库

配置web.xml 文件加载核心控制器

web.xml 代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>chapter06</display-name>
  
<!-- Struts2添加核心控制器 -->
  <filter>
  	<filter-name>Struts2</filter-name><!--  过滤器名字  -->
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>Struts2</filter-name><!-- 过滤器名 -->
  	<url-pattern>/*</url-pattern><!-- 匹配所有请求 -->
  </filter-mapping>
  
  <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>
</web-app>

开发视图层

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
	<form name="form1" method="post" action="login.action">
		用户名<input type="text" name="loginName"><br>
		密&nbsp;&nbsp;码<input type="password" name="loginPwd"><br>
		<input type="submit" value="登录"><br>
		<input type="reset" value="取消"><br>
	</form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> 登录成功页面</title>
</head>
<body>
 你好 ${param.loginName },来到Struts世界!
</body>
</html>

开发业务控制器

LoginAction.java

package com.zdy.chapter06.action;

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{
	private String loginName;
	private String loginPwd;
	
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	
	public String getLoginPwd() {
		return loginPwd;
	}
	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
	
	@Override
	public String execute() throws Exception {
		// 登录用户名和密码判断,此处暂时不访问数据库
		if("admin".equals(loginName) && "123".equals(loginPwd)){
			return "success";
		}else{
			return "input";
		}
		
	}
	

}

配置业务控制器

struts2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 定义restaurant包, 继承Struts 2的默认包  -->
	<package name="chapter06" namespace="/" extends="struts-default">
		<action name="login" class="com.zdy.chapter06.action.LoginAction">
			<result name="success">/ch06/index.jsp</result>
			<result name="input">/ch06/login.jsp</result>
		</action>	
	</package>	
</struts>

部署项目。发布成功后在浏览器地址栏输入 http://localhost:8080/chapter06/ch06/login.jsp
输入 用户名 admin 和密码 123,如果信息正确,会显示
在这里插入图片描述
接下来介绍一下

使用Struts2实现登录功能的处理过程

  1. 通过浏览器,运行登录页面。输入用户名和密码,单击登录按钮,向服务器提交用户名和密码信息。
  2. 读取web.xml 配置文件,加载 Struts2 的核心控制器StrutsPrepareAndExecuteFilter,对用户进行拦截。
  3. 根据用户提交表单中的action.在Struts.xml 配置文件中找到匹配的Action 配置,这里会查找name属性为login 的action 配置,并且把已经拦截的请求发送给对应的LoginAction 业务类来处理。
  4. 在Struts.xml 文件中没有指定action 元素的method 属性值,此时系统会调用默认方法execute()来访问数据库完成对客户端的登录请求。若登录成功,则返回success字符串,否则返回input字符串。
  5. 根据返回结果,在struts.xml 配置文件中查找相应的映射,配置LoginAction时,指定了 < result name=“success”>/ch06/index.jsp
    < result name=“input”>/ch06/login.jsp ,所以,返回success 转向index.jsp .反之,转向login.jsp
发布了88 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_21344887/article/details/88538400