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 HTML标签简化开发
<%@ 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>
状态:0 – 显示为“禁用”, 1 – 显示为“正常”
新建实体类User:
四个属性username,password,birthday,status 默认构造方法,带参构造方法;
在LoginAction
登陆成功后设置UserList
List<User> userList = userBiz.getAllUser();request.setAttribute("userList", userList);
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>
部署登陆,成功显示;
- Struts1基础、使用Struts实现登录、使用Struts HTML标签简化开发
- Struts1.2开发技巧二-----------------Struts标签的使用
- 使用Struts提供的HTML标签库
- 使用Struts提供的HTML标签库
- 使用 Struts 提供的 HTML 标签库
- 使用 Struts 提供的 HTML 标签库
- Struts Html:Checkbox 标签使用技巧
- HTML中使用struts标签的方法
- struts中html:options标签的使用
- struts 标签 HTML:MESSAGES 使用范例
- Struts -- html:link 标签的使用
- 【struts】html:errors标签的使用
- Struts -- html:link 标签的使用(转)
- Struts html标签的使用及说明
- struts1.0 struts标签
- Struts1.3——使用MyEclipse集成工具开发Struts
- 【开发框架】struts标签使用举例-logic
- struts开发环境搭建与标签使用
- 手工建数据库的步骤(实验总结)
- iOS开发之保存图片到手机相册
- ORA-01033: ORACLE initialization or shutdown in progress --手动删除表空间 DBF 后无法登陆问题
- java线程的状态
- myBatis+oracle11g insert语句报错17023 不支持的特性
- Struts1基础、使用Struts实现登录、使用Struts HTML标签简化开发
- Objective-C中的@property和@synthesize用法
- redis 安装(mac os 下教程)
- 让自己or档翻译塞今天险
- 桥梁模式
- 教你兼St语簧测朋友浪
- 如何wiSw所示的游蹦
- 这才是70言吸,当电脑钟
- 如何做完成这件注射删除渤