使用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 框架写用 ...
随机推荐
- redis linux 基本命令
找到一个哥们 写的都是一步步打基础的学习东西 不光是知识也是学习方式 都值得学习.. reids 传送们-->> http://xuelianbobo.iteye.com/category ...
- iOS中@class #import #include 简介
[转载自:http://blog.csdn.net/chengwuli125/article/details/9705315] 一.解析 很多刚开始学习iOS开发的同学可能在看别人的代码 ...
- ubuntu firefox 选中变成了删除
在ubuntu下 我的firefox浏览器出现了问题.描述: 1.在firefox中所有能够删除的文字只要选中就自动删除了. 终端中 ibus-setup勾掉在应用窗口中启用内嵌编辑模式
- today's learning of english 2
1..fumbling 缓慢 笨拙的 2.dancing with impatience in the chekout line sighed exasperation 在安全线外 ...
- PHP安全编程:会话数据注入 比会话劫持更强大的攻击(转)
一个与会话暴露类似的问题是会话注入.此类攻击是基于你的WEB服务器除了对会话存储目录有读取权限外,还有写入权限.因此,存在着编写一段允许其他用户添加,编辑或删除会话的脚本的可能.下例显示了一个允许用户 ...
- mcrypt.h not found. Please reinstall libmcrypt
在centos上对php5.6进行源码安装的时候, 出现了如题所示错误提示, 原因是由于centos源不能安装libmcrypt-devel,由于版权的原因没有自带mcrypt的包 解决办法使用php ...
- HDU5317
题意:定义一个数K,最小质因数形式为K = a*b*c形式(如12 = 2*2*3),相同只取一个(所以12只能取2,3两个,既F[12]=2)给L,R区间,找出区间内最大的F[x](L<=x& ...
- Java编程思想-注解生成外部例子代码
如果本文帮助到您,请点击下面的链接,这是本人的网站,以示鼓励,谢谢!链接绝对安全! 本人的网站 java注解属于java中高大上的功能,许多开源框架都使用了java注解的功能.比如spring,hib ...
- ASPNET5的依赖注入
ASP.NET5设计的时候就是以DI为基础的,它可以利用内建的框架在Startup类的方法中,把依赖注入进去.应用服务也可以被配置的注入.默认的服务容器提供一些基本的功能,它并不打算代替现代主流的DI ...
- Oracle创建表空间以及用户,并授权
//创建临时表空间 create temporary tablespace testtemp tempfile 'D:/app/Administrator/oradata/testdata/te ...