Struts1基础、使用Struts实现登录、使用Struts HTML标签简化开发

来源:互联网 发布:互联网行业数据 编辑:程序博客网 时间:2024/05/17 20:30

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)

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

3、开发并配置LoginAction

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

@Overridepublic 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-mappingaf=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;}elsereturn 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

0 0
原创粉丝点击