使用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 框架写用 ...
随机推荐
- 4th day
老师讲的好快啊... /* 建一个innodb类型且字符集为utf8的表,其中包括以下类型的字段:int(自增长),float,char,varchar,datetime,date,text,enum ...
- Linux 和 Windows 下对long long的输出
以前一直用__int64来识别windows还是Linux,可是发现HDU好像不认 看到wzy用的UNIX可以. UNIX是Linux下定义的,具体是什么可以去百度. 那么就可以 #ifdef UNI ...
- Appium官网Introduction
英文官网:http://appium.io/introduction.html?lang=zh Appium 简介 Appium是一个开源的自动化测试工具,其支持iOS和安卓平台上的原生的,基于移动浏 ...
- 第十四章 红黑树——C++代码实现
红黑树的介绍 红黑树(Red-Black Tree,简称R-B Tree),它一种特殊的二叉查找树.红黑树是特殊的二叉查找树,意味着它满足二叉查找树的特征:任意一个节点所包含的键值,大于等于左孩子的键 ...
- vim 学习相关记录
VIM 相关内容****************** vim 的三个模式: 编辑模式 --> 输入模式 --> 末行模式 编辑模式: 通常键入键盘值被理解成一个操作; 如: dd(删除行) ...
- Asp,题目
1. 简述 private. protected. public. internal 修饰符的访问权限.答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成员 ...
- urlwrite伪静态(SAE、PHP、JSP)
在SAE里,直接配置config.yaml文件,文件可以配置的内容包含: 目录默认页面 自定义错误页面 压缩 页面重定向 页面过期 设置响应Header的Content-Type appname: x ...
- MIME协议
转:http://blog.csdn.net/flfna/article/details/5048290 MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候 ...
- 网站项目:让一般处理文件.ashx的代码有折叠功能(#region)
注意:该方法用于网站项目.但对于其他类型的项目有一定的参考作用. 1.首先在你想被别人访问的位置新建一个ashx文件,如/System/xxx.ashx. 新建xxx.ashx的代码如下: [csha ...
- Android 自定义日历
好久没来写博客了,这半年多发生了好多的事情,废话不多说,今天在公司里比较闲在,写一篇最近写的公司用到的控件——日历控件. 控件的功能比较少,根据需求只有选择开始时间和结束时间并返回时间段. 效果图如下 ...