使用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 框架写用 ...
随机推荐
- javascript变量 数组 对象
一 变量 1.全局变量和局部变量 在JavaScript中同一个变量可以反复赋值,而且可以是不同类型的变量,但是要注意只能用var声明一次.这种变量类型不固定的语言称为动态语言,与之对应的静态语言,如 ...
- UTR#2 T1
题意:给定一个n,以下n个数(假定为fi),要求构造一个n个数的序列,使得这个序列每一个位置的最大上升子序列的长度等于对应的fi. 其实这道题是个很简单的题,之前7月也在BC上做到过,为什么要写呢,因 ...
- nvl,空时的推断和取值
nvl NVL的概念 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:假设string1为NULL,则NVL函数返回replace_wi ...
- 实现O(1)时间复杂度带有min和max 函数的栈
仅仅是演示实现.不考虑栈使用的数据结构是vector 还是其它容器. 代码例如以下 #include <iostream> #include <vector> using na ...
- jquery自定义插件来实现分页的效果
本节将介绍如何定义自己的jquery插入,实现分页效果,话不多说,.看看达到的效果: 分页插件 实现的代码例如以下: <!DOCTYPE HTML PUBLIC "-//W3C//DT ...
- iTunes 11.2更新下载:改善播客阅读
昨日,苹果公布了iTunes 11.2更新,新版别改善了播客阅读的方法,让用户能够在新的"未播映的"标签中疾速找到没有赏识的单集,其他功用包括主动删去现已播映的单集,并可在&quo ...
- SSL证书制作
1.创建根证书秘钥文件(自己做CA)root.key: openssl genrsa -out root.key -aes256 2048 2.创建根证书的申请文件root.csr openssl r ...
- Memento 备忘录 快照模式
简介 定义: 在不破坏封装的前提下,捕获一个对象的[内部状态],并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态. 角色: 发起人Originator:要被备份的成员,它提供一创 ...
- codevs1404字符串匹配
/* 无奈我改了那么久还是看的题解 首先跑一边kmp 几下ans[p]表示总共匹配到长度p的次数 这些不一定都是恰好到p 所以在处理一下 ans[p]通过处理变成 所有的匹配到长度p的次数 最后答案就 ...
- HTML5 Canvas Text文本居中实例
1.代码: <canvas width="700" height="300" id="canvasOne" class="c ...