使用Struts1完成用户登录功能
1、Struts框架
框架(framework):就是一系列代码和开发模式的整合,使用框架后,所有开发人员都会按照框架提供的规范进行开发,使代码更容易维护和扩展。
使用框架的优点:
1) 易于维护扩展
2) 简化代码
Struts框架是MVC模式的具体实现框架,实现针对MVC模式中的Servlet以及jsp页面代码的简化。
JSP + Servlet 的执行流程:
jsp à web.xml中查找<servlet-mapping>找到进入哪个Servlet à 执行doGet或doPost方法,接收参数,验证,整合,调用service,设置属性,跳转 à 返回jsp。
Struts执行流程中主要修改了Servlet部分,不需要再编写Servlet,但需要建立Action和ActionForm,将Servlet中主要实现的功能也拆分为两部分,其中对于参数的处理交给ActionForm来执行,其他操作由Action实现。
在jsp页面上,不再使用JSTL,改为Struts-Taglib,可以替代JSTL标签完成循环,判断,格式化等操作,并扩展了新的功能,替代原有页面表单,形成动态表单,支持自动回填功能。
Struts不对数据库操作代码产生任何影响,DAO还是使用原有的JDBC。
2、使用Struts完成用户登录功能
假设用户输入用户名为zhangsan,密码为123表示登陆成功,否则登陆失败。
建立项目,加入Struts1.3支持。
加入支持后,项目中多出以下内容:
1) src下的资源文件(ApplicationResources.properties)
2) 支持类库
3) struts-config.xml
web.xml中加入ActionServlet配置
<?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">
<display-name /> <!-- Struts的配置 -->
<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>
如果想不使用MyEclipse加入支持,可以从apache官方网站上下载开发包,从开发包中找到这些配置,并加入项目。
在index.jsp中导入struts的html标签,并完成登陆表单
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%
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>登陆页面</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>
<html:form method="post" action="login.do" >
<br>用户ID:<html:text property="userid"></html:text>
<br>密码:<html:password property="password"></html:password>
<br><html:submit value="登陆"></html:submit>
</html:form>
</body>
</html>
改为标签形式,其中property就是之前普通元素的name。
下面建立提交后接收信息和处理的ActionForm与Action


path表示进入此Action以及ActionForm的提交路径,以 / 开头。
Input Source表示出错后自动跳转的错误页路径。
Finish完成后,会在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 >
<!-- 配置的LoginForm -->
<form-bean name="loginForm" type="lq.wangzhen.struts.form.LoginForm" />
</form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<!-- Action配置选项,指定对应的跳转路径path,错误路径input,和对应的form name -->
<action
attribute="loginForm"
input="/index.jsp"
name="loginForm"
path="/login"
scope="request"
type="lq.wangzhen.struts.action.LoginAction"
cancellable="true" />
</action-mappings> <message-resources parameter="lq.wangzhen.struts.ApplicationResources" />
</struts-config>
先编写ActionForm接收参数,并验证。
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package lq.wangzhen.struts.form; 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;
public class LoginForm extends ActionForm {
/**
* 变量名称和表单中的变量名称一致,会自动的进行接收,但要编写对应的setter和getter方法
*/
private String userid;
private String password; public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
//验证用户名和密码是否为空
ActionErrors errors = new ActionErrors();
if(userid == null || "".equals(userid.trim())){
errors.add("useridErr",new ActionMessage("userid.null"));
}
if(password == null || "".equals(password.trim())){
errors.add("passwordErr", new ActionMessage("password.null"));
}
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
validate方法会在接收完参数后由Struts自动调用,验证返回的ActionErrors里如果包含了错误信息,则Struts会自动根据配置的错误页跳转回页面,而不进入Action。
错误信息通过ActionMessage,从资源文件中查找。
ApplicationResources.properties
userid.null=\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A\uFF01
password.null=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A\uFF01
资源文件中不允许出现中文,因此必须对中文进行转码。
在index.jsp中,提示错误信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%
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>登陆页面</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>
<html:form method="post" action="login.do" >
<br>用户ID:<html:text property="userid"></html:text>
<font color="red"><html:errors property="useridErr"/></font> <!-- 配置用户名错误信息 -->
<br>密码:<html:password property="password"></html:password>
<font color="red"><html:errors property="passwordErr"/></font> <!-- 配置密码错误信息 -->
<br><html:submit value="登陆"></html:submit>
</html:form>
</body>
</html>
在property中传入之前加入错误时设置的key值,就可以取得该错误信息并显示。
如果没有加property,则会取得所有错误信息并显示。
进入Action,在Action中实现其他的操作。
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package lq.wangzhen.struts.action; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import lq.wangzhen.struts.form.LoginForm; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage; public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
if(loginForm.getUserid().equals("wangzhen") && loginForm.getPassword().equals("123")){
//登陆成功
request.getSession().setAttribute("userid", loginForm.getUserid());
//跳转到成功也,这里只给出名称,具体的路径配置到struts-config.xml中
return mapping.findForward("success");
}else{
//登陆失败,自动返回到失败页
//还需要保存错误信息
ActionErrors errors = new ActionErrors();
errors.add("loginErr", new ActionMessage("login.err"));
//手工保存错误信息
super.saveErrors(request, errors);
return mapping.getInputForward();
}
}
}
这里配置了跳转路径,需要修改struts-config.xml,将success的路径配置上。
<!-- Action配置选项,指定对应的跳转路径path,错误路径input,和对应的form name -->
<action
attribute="loginForm"
input="/index.jsp"
name="loginForm"
path="/login"
scope="request"
type="lq.wangzhen.struts.action.LoginAction"
cancellable="true" >
<forward name="success" path="/pages/success.jsp"></forward>
</action>
name是跳转路径名称,就是Action中findForward()方法中传入的值
path是具体跳转路径,必须以 / 开头。
在index.jsp中提示错误信息
<body>
<font color="red"><html:errors property="loginErr"/></font>
<html:form method="post" action="login.do" >
<br>用户ID:<html:text property="userid"></html:text>
<font color="red"><html:errors property="useridErr"/></font> <!-- 配置用户名错误信息 -->
<br>密码:<html:password property="password"></html:password>
<font color="red"><html:errors property="passwordErr"/></font> <!-- 配置密码错误信息 -->
<br><html:submit value="登陆"></html:submit>
</html:form>
</body>
使用Struts1完成用户登录功能的更多相关文章
- Struts2整合Hibernate3实现用户登录功能
所用技术:struts2 ,hibernate,jsp,mysql 本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰 ...
- JavaWeb学习记录(六)——用户登录功能
使用JDBC.spring框架.servlet实现一个简单的用户登录功能. 一.mySql数据库 SET FOREIGN_KEY_CHECKS=0; -- ---------------------- ...
- 实现Web上的用户登录功能
关于如何实现web上的自动登录功能 文章来源http://coolshell.cn/articles/5353.html Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能 ...
- 你会做Web上的用户登录功能吗?
Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能.下面的文章告诉大家这个功能可能并没有你所想像的那么简单,这是一个关 ...
- 利用MYSQL的函数实现用户登录功能,进出都是JSON(第二版)
利用MYSQL的函数实现用户登录功能,进出都是JSON(第二版) CREATE DEFINER=`root`@`%` FUNCTION `uc_session_login`( `reqjson` JS ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(五)——实现注册功能
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(四)——对 run.py 的调整
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
- 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化
使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(二)——使用蓝图功能进行模块化 使用 Flask 框架写用 ...
随机推荐
- 谈NOT IN和Exists
表1: test001 select * from test001
- 第四篇:SQL
前言 确实,关于SQL的学习资料,各类文档在网上到处都是.但它们绝大多数的出发点都局限在旧有关系数据库里,内容近乎千篇一律.而在当今大数据的浪潮下,SQL早就被赋予了新的责任和意义. 本篇中,笔者将结 ...
- [转] Java中的容器
在书写程序的时候,我们常常需要对大量的对象引用进行管理.为了实现有效的归类管理,我们常常将同类的引用放置在同一数据容器中. 由于数据容器中存放了我们随时可能需要使用到的对象引用,所以一般的数据容器要都 ...
- 获取Html中所有img的src
/// <summary> /// 获取所有Img中的Src /// </summary> /// <param name="htmlText"> ...
- phpmyadmin安装出错,缺少 mysqli 扩展。请检查 PHP 配置
下载了个phpmyadmin最新版本的,始终显示这样的内容,求助.如何解决哈?>缺少 mysqli 扩展.请检查 PHP 配置. <a href="Documentation.h ...
- contains选择器
有这样一个问题: 一个列表里面,很多option,但是在不知道value,只知道他的内容的时候,怎么进行选择,比如: 北京市天津市上海市重庆市 在不知道他的value和index的时候,选择北京市,能 ...
- 使用WMI来控制Windows目录 和windows共享机制
1.使用WMI来控制Windows目录 本文主要介绍如何使用WMI来查询目录是否存在.文件是否存在.如何建立目录.删除目录,删除文件.如何利用命令行拷贝文件,如何利用WMI拷贝文件 using Sys ...
- WPF概述
一.概述 WPF(Windows Presentation Foundation,视窗展示[呈现.展现.显示.表示]基础)是Windows的新一代图形子系统,他为开发人员提供了统一的编程模型,可用于构 ...
- (转)PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
在看帝国cms的connect.php是发现第一句是error_reporting(E_ALL ^ E_NOTICE);以前也没注意过这个语句,知道是设置错误提示的,但不清楚具体怎样设置使用.下面从网 ...
- IOS开发常用的linux命令
pwd 在Linux层次结构中,用户可以在被授权的任意目录下利用mkdir命令创建新目录,也可以利用cd命令从一个目录转换到另一个目录.然而,没有提示符来告知用户目前处于哪一个目录中.想要知道当前所处 ...