Struts 1基础

为什么重拾Struts 1

曾经是最主流的MVC框架

市场份额依然很大

很多遗留系统中依旧使用

维护和升级都需要熟悉Struts 1

与Struts 2相比

编码、配置繁琐

侵入性强

例子:使用Struts实现登录

登录失败

返回登录页面,提示失败

登录成功

保存当前登录用户到Session

转到成功页面,显示欢迎信息

 开发步骤:

1、添加Struts到项目

添加jar包和struts-config.xml

在web.xml中配置ActionServlet

2、开发并配置ActionForm

3、开发并配置LoginAction

4、创建并编写页面

5、调试运行

新建web project项目:Struts1Demo

右击项目添加struts1支持

配置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">
<!-- 在/org/apache/struts/resources/struts-config_1_3.dtd 第24行  -->
<struts-config>
	<!-- Form -->
	<form-beans >
		<form-bean name="userLoginForm" type="com.demo.form.LoginForm"></form-bean>
	</form-beans>

	<!-- Action -->
	<action-mappings >
		<action name="userLoginForm" path="/login" type="com.demo.action.LoginAction" scope="request">
			<forward name="success" path="/success.jsp"></forward>
			<forward name="input" path="/index.jsp"></forward>
		</action>
	</action-mappings>

</struts-config>

在web.xml中配置ActionServlet

<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>
  </servlet>

2.开发并配置ActionForm

新建类LoginForm extends ActionForm 在com.demo.form下

私有属性username,password和getter,setter方法;

重写reset方法(输入execute 按alt+/ 选择参数有 HttpServletRequest)

	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		//每次提交表单的时候都会调用一次
		this.username=null;
		this.password=null;
	}

3、开发并配置LoginAction

新建LoginAction extends Action在com.demo.action下,重写execute方法(输入execute 按alt+/ 选择参数有 HttpServletRequest)

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		//做登陆
		ActionForward af = null;
		LoginForm lf = (LoginForm) form;
		UserBiz userBiz = new UserBizImpl();
		if(userBiz.login(lf.getUsername(),lf.getPassword())){
			//登陆成功
			request.getSession().setAttribute("loginUser", lf.getUsername());
			//跳转
			//mapping 是配置文件struts-config.xml的 action-mapping
			af=mapping.findForward("success");
		}else{
			request.setAttribute("message", "用户名密码错误");
			af=mapping.findForward("input");
		}
		return af;
	}

新建UserBiz接口和它的实现类UserBizImpl添加login方法:

	public boolean login(String username, String password) {
		//直接用模拟,不访问数据库
		if("admin".equals(username)&&"admin".equals(password)){
			return true;
		}else
		return false;
	}

4、创建并编写页面

index.jsp

  <body>
  	<font color="red">${message }</font>
  	<form action="login.do" method="post">
  		<table>
  			<tr>
  				<td>用户名</td>
  				<td><input name="username"/> </td>
  			</tr>
  			<tr>
  				<td>密码:</td>
  				<td><input name="password" type="password"/> </td>
  			</tr>
  			<tr>
  				<td><input type="submit" value="登陆"/> </td>
  				<td><input value="重置" type="reset"/> </td>
  			</tr>
  		</table>
  	</form>
  </body>

success.jsp

  <body>
    欢迎:${loginUser }登陆!
  </body>

5、调试运行

Struts对MVC的实现

Struts核心组件

控制器组件

ActionServlet

1. 由Struts提供:org.apache.struts.action.ActionServlet

2. 本身是一个Servlet,需要在web.xml中配置

Action -- Action Bean

1. 封装某一类客户操作,如:登录、注销

2. 需要在struts-config.xml中配置

3. 继承自org.apache.struts.action.Action,实现execute方法

4. execute方法的参数

mapping

form

request、response

5. execute方法的返回值类型:ActionForward

视图组件

ActionForm -- Form Bean

1) 封装页面提交的数据

2) 继承自org.apache.struts.action.ActionForm

3) 需要在struts-config.xml中配置

4) 从页面获得输入- 属性与表单域name属性值一致

loginForm.getUsername();

loginForm.getPassword();

其他视图组件:

JSP、JSTL、EL、自定义标签

Struts标签

模型组件

Struts对模型组件的实现没有任何限制
一般为:[User]Biz接口、[User]BizImpl类、[User]DAO接口、[User]DAOHibImpl类

Struts运行过程

使用Struts HTML标签简化开发

index.jsp


<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>


	<html:form action="login" method="post">
		<table>
			<tr>
				<td>用户名</td>
				<td><html:text property= "username" /></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><html:password property= "password" /></td>
			</tr>
			<tr>
				<td><html:submit value="登陆" /></td>
				<td><html:reset value="重置" /></td>
			</tr>
		</table>
	</html:form>

实现显示用户列表功能
日期格式:yyyy年mm月dd日

状态:0 – 显示为“禁用”, 1 – 显示为“正常”



新建实体类User:

 四个属性username,password,birthday,status 默认构造方法,带参构造方法;



在LoginAction

登陆成功后设置UserList
List<User> userList = userBiz.getAllUser();
request.setAttribute("userList", userList);
模拟两个User 在UserBizImpl
	public List<User> getAllUser() {
		List<User> userList = new ArrayList<User>();
		userList.add(new User("admin", "admin", new Date(), 1));
		userList.add(new User("admin1", "admin1", new Date(), 0));
		return userList;
	}

在success.jsp


<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>

Body里面:


<body>
    欢迎:${loginUser }登陆!
    <table border="1px" >
	    <thead>
	    	<tr>
		    	<th>用户名</th>
		    	<th>生日</th>
		    	<th>状态</th>
	    	</tr>
	    </thead>

	    <tbody>
	  	  <!-- 结合name和property属性查找JavaBean -->
	     <logic:iterate id="u" name="userList">
	    	<tr>
	    		<td>
	    			<!-- bean:write标签用于输出页面输出 -->
	    			<bean:write name="u" property="username"/>
	    		</td>
	    		<td>
	    			<bean:write name="u" property="birthday" format="yyyy-MM-dd"/>
	    		</td>
	    		<td>
	    			<!-- <bean:write name="u" property="status" format="#,###"/> -->
	    			<logic:equal value="1" name="u" property="status">正常</logic:equal>
	    			<logic:equal value="0" name="u" property="status">禁用</logic:equal>
	    		</td>
	    	</tr>
	    </logic:iterate>
	    </tbody>
    </table>
  </body>

部署登陆,成功显示;

http://pan.baidu.com/s/1mgsRkX6


Struts1基础、使用Struts实现登录、使用Struts HTML标签简化开发的更多相关文章

  1. SSH框架之Struts(4)——Struts查漏补缺BeanUtils在Struts1中

    在上篇博客SSH框架之Struts(3)--Struts的执行流程之核心方法,我们提到RequestProcessor中的processPopulate()是用来为为ActionForm 填充数据.它 ...

  2. Struts1入门实例(简单登录)

    Struts1入门实例(简单登录) 现在开始加入公司的核心项目,但由于项目开发比较早,所以使用的技术不是很新潮,前台用的还是struts1. 虽然不是什么新技术,但仍可以从中学到好多东西的.花了一个晚 ...

  3. struts(二)——struts框架实现的基本原理

    上一篇文章,我们介绍了MVC框架的基本原理,并指出了这个基本框架中存在大量if…else的问题.今天我们来介绍一下struts框架,让struts解决这个问题. 首先,看一下粗略的时序图: Actio ...

  4. 【转】Struts1.x系列教程(6):Bean标签库

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...

  5. Struts1应用、实现简单计算器、使用DispatchAction、显示友好的报错信息、使用动态Form简化开发

    实现简单的支持加.减.乘.除的计算器 复制一份Struts1Demo修改:Struts1Calc 方案1: Struts1Calc 创建ActionForm: CalcForm extends Act ...

  6. Android基础新手教程——1.2.1 使用Eclipse + ADT + SDK开发Android APP

    Android基础新手教程--1.2.1 使用Eclipse + ADT + SDK开发Android APP 标签(空格分隔): Android基础新手教程 1.前言 这里我们有两条路能够选,直接使 ...

  7. 少量代码设计一个登录界面 - .NET CORE(C#) WPF开发

    微信公众号:Dotnet9,网站:Dotnet9,问题或建议:请网站留言, 如果对您有所帮助:欢迎赞赏. 少量代码设计一个登录界面 - .NET CORE(C#) WPF开发 阅读导航 本文背景 代码 ...

  8. struts json登录

    1.struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts P ...

  9. Ajax+Struts做登录判断

    Action类里: /* * 登录 */ public ActionForward doLogin(ActionMapping mapping,ActionForm form,HttpServletR ...

随机推荐

  1. jQuery的ajax学习

    jQuery是一个非常常见的JavaScript库,可是,突然发现其实自己大多数时候,潜意识里面对它其实是视而不见的,比如它的ajax,不是没用过,每次使用,要不,是直接套用已有的格式,要不直接从官网 ...

  2. java开发----自定义对象,重写equals方法

    javaweb开发中,用到了好多自定义对象,这时候如果不重写equals方法,很多时候都会返回false, 因此我们必须习惯重写这个方法. 重点: 1.equals比较俩对象时比较的是对象引用是否指向 ...

  3. preg_replace引发的phpmyadmin(4.3.0-4.6.2)命令执行漏洞

    编辑器坏了 (:  今天看到这个phpmyadmin的代码执行,https://www.waitalone.cn/phpmyadmin-preg_replace-rce.html 记录一下:preg_ ...

  4. boot.img格式文件拆解实例结构解析

    以msm8226为例,讲解android源码编译生成boot.img的结构.boot.img包括boot.img header.kernel以及ramdisk文件系统.下面是对boot.img的结构进 ...

  5. [SDOI2016]游戏

    Description Alice 和 Bob 在玩一个游戏. 游戏在一棵有 n 个点的树上进行.最初,每个点上都只有一个数字,那个数字是 123456789123456789. 有时,Alice 会 ...

  6. [USACO17FEB]Why Did the Cow Cross the Road I S

    题目描述 Farmer John's cows are trying to learn to cross the road effectively. Remembering the old " ...

  7. bzoj 1488: [HNOI2009]图的同构

    Description 求两两互不同构的含n个点的简单图有多少种. 简单图是关联一对顶点的无向边不多于一条的不含自环的图. a图与b图被认为是同构的是指a图的顶点经过一定的重新标号以后,a图的顶点集和 ...

  8. Codeforces Round #464 F. Cutlet

    Description 题面 有\(2*n\)的时间,去煎一块肉,肉有两面,你需要在特定的时间内翻转,使得每一面都恰好煎了\(n\)分钟,你有\(k\)次翻转的机会,每一次表示为一段时间 \([L_i ...

  9. C++Primer学习——const

    Const int size = 512; 在编译的时候,编译器会把用到该变量的地方全部替换成对应的值. const&可以绑定字面值,所以当用常量引用绑定一个常量时,是否可以看成那个值在编译阶 ...

  10. ●BZOJ 3676 [Apio2014]回文串

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题解: 后缀数组,Manacher,二分 首先有一个结论:一个串的本质不同的回文串的个 ...