struts(三)——struts框架实现登录示例
前两篇讲解了struts框架的基本实现,自己感觉讲的也有些枯燥,今天拿登录的例子来做一个实现。
1、新建一个javaweb项目,并将struts的jar包拷贝到WebRoot/WEB-INF/lib下。
2、拷贝完jar包还不能进行工作,用户的请求必须达到ActionServlet,我们才能通过struts处理请求,所以还需要将ActionServlet配置在web.xml文件里面。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3、建立actionForm,表单上的用户名和密码,必须与actionForm中属性的get/set方法后半部分一致。
package com.xxjstgb.struts;
import org.apache.struts.action.ActionForm;
/**
* 登录ActionForm,负责表单收集数据
* @author liuzhengquan
*
*/@SuppressWarnings("serial")
public class LoginActionForm extends ActionForm {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4、建立LoginAction,这里需要继承struts的Action类。处理Model层的逻辑,并返回转向信息。
package com.xxjstgb.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 登录Action
* 负责取得表单数据、调用业务逻辑、返回转向信息
* @author liuzhengquan
*
*/
public class LoginAction extends Action { @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf=(LoginActionForm)form;
String userName=laf.getUserName();
String password=laf.getPassword();
UserManager userManager=new UserManager();
try {
userManager.login(userName, password); return mapping.findForward("success");
} catch (UserNoFoundException e) {
e.printStackTrace();
request.setAttribute("msg", "用户不能找到,用户名称=【"+userName+"】");
}catch (PasswordErrorException e) {
e.printStackTrace();
request.setAttribute("msg", "密码错误");
}
return mapping.findForward("error");
}
}
5、配置struts-config.xml文件。在struts(一)里面我们通过if…else判断截取的URL,根据相应的URL,调用相应的Action。在struts框架里面已经对此做了简单的封装,我们只需要将action和actionForm配置在struts-config.xml文件里面。通过映射他们的关系,来实现匹配工作,替代了原来的if…else语句。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="LoginForm" type="com.xxjstgb.struts.LoginActionForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/login"
type="com.xxjstgb.struts.LoginAction"
name="LoginForm"
scope="request">
<forward name="success" path="/login_success.jsp"></forward>
<forward name="error" path="/login_failed.jsp"></forward>
</action>
</action-mappings>
</struts-config>
6、访问
<body>
<form action="login.do" method="post">
用户名:<input type="text" name="userName"><br>
密 码:<input type="password" name="password"> <br>
<input type="submit" value="登录">
</form>
</body>
至此,通过struts框架完成了登录实例。
struts(三)——struts框架实现登录示例的更多相关文章
- 在Eclipse中使用Struts和Hibernate框架搭建Maven Web项目
前言 学习使用Java还是2012年的事情,刚开始学习的Java的时候,使用的是MyEclipse工具和SSH框架.初学者适合使用MyEclipse,因为他将struts.Spring和Hiberna ...
- 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验
一,使用Struts2标签库简化表单 在文章[基于Struts2框架实现登录案例]的基础上,通过使用Struts标签库可以简化登录页面login2.jsp <%@ page language=& ...
- 基于Struts2框架实现登录案例
一,准备工作 1)新建web项目,并导入Struts2jar文件和配置web.xml文件. struts2 jar文件 web.xml文件 <?xml version="1.0&qu ...
- SSH三种框架及表示层、业务层和持久层的理解
Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...
- python 全栈开发,Day87(ajax登录示例,CSRF跨站请求伪造,Django的中间件,自定义分页)
一.ajax登录示例 新建项目login_ajax 修改urls.py,增加路径 from app01 import views urlpatterns = [ path('admin/', admi ...
- Struts2---配置文件讲解及简单登录示例
bean 用于创建一个JavaBean实例 constant 用于Struts2默认行为标签 <!-- 配置web默认编码集,相当于HttpServletRequest.setChartacte ...
- SSH三种框架及表示层、业务层和持久层的理解(转)
Struts(表示层)+Spring(业务层)+Hibernate(持久层) SSH:Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts:Struts是一个表示 ...
- Struts2中有关struts-default.xml,struts.xml,struts.properties文件详解
1) struts-default.xml 这个文件是struts2框架默认加载的配置文件.它定义struts2一些核心的bean和拦截器. <?xml version="1.0&qu ...
- JHipster生成微服务架构的应用栈(三)- 业务微服务示例
本系列文章演示如何用JHipster生成一个微服务架构风格的应用栈. 环境需求:安装好JHipster开发环境的CentOS 7.4(参考这里) 应用栈名称:appstack 认证微服务: uaa 业 ...
随机推荐
- GDI 总结三: CImage类使用
前言 CImage类是基于GDI+的.可是这里为什么要讲归于GDI? 主要是基于这种考虑: 在GDI+环境中,我们能够直接使用GDI+ ,没多少必要再使用CImage类 可是,假设再 ...
- 大数据实时处理-基于Spark的大数据实时处理及应用技术培训
随着互联网.移动互联网和物联网的发展,我们已经切实地迎来了一个大数据 的时代.大数据是指无法在一定时间内用常规软件工具对其内容进行抓取.管理和处理的数据集合,对大数据的分析已经成为一个非常重要且紧迫的 ...
- android在其他线程中访问UI线程的方法
1.Activity.runOnUiThread( Runnable ) 2.View.post( Runnable ) 3.View.postDelayed( Runnable, long ) 4. ...
- 分分钟教会你使用HTML写Web页面
在学习怎样使用HTML编写网页之前,我们必须先搞清楚什么是HTML?当然了不是系统的给大家介绍HTML的前世今生,假设对其身世感兴趣的小伙伴能够去问度娘,她会给你想要的答案. 所谓HTML,就是我们常 ...
- HTML5动态分页效果代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- 基于FPGA的红外遥控解码与PC串口通信
基于FPGA的红外遥控解码与PC串口通信 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<电子设计EDA>的课程设计作业(呵呵,这个月都拿 ...
- MPICH3环境配置
最新版的mpich简化了运行方式,不再提供mpd开头的命令,只需要一个mpiexec即可启动mpi运行环境,方便了mpi编程.源代码下载地址:http://www.mpich.org/download ...
- VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 9月mob(ShareSDK)活动预告,这个秋天非常热
9月秋天来临,广州的天气依旧非常热,广州的活动氛围更热~ 先有GMGC B2B对接会在广州创新谷,再有上方网TFC全球移动游戏开发人员大会来袭,游戏圈的火越烧越旺,成都GMGDC全球移动游戏开发人员大 ...