深入浅出学习struts1框架(五)--正式进入struts1框架学习,从一个实例开始

 之前写了四篇博客都是struts1框架学习之前的引子,主要就是从mvc的一个实例开始,慢慢重构出一个struts1框架雏形,通过这个雏形来引出我们要学习的struts1框架。四篇博客如下:

1、《深入浅出学习Struts1框架(一):一个简单mvc模式代码示例开始

2、《深入浅出学习Struts1框架(二):重构MVC模式代码中跳转路径和业务逻辑

3、 《深入浅出学习Struts1框架(三):彻底去掉TestServlet中的字符串和if-else语句块》

4、《深入浅出学习struts1框架(四):从MVC模式代码认识struts1框架


      在深入浅出学习struts1框架(四):从MVC模式代码认识struts1框架 博客中解释了框架是什么以及struts1框架的结构,通过上篇博客我们明白了框架无非就是一个应用程序的半成品,是一系列的高复用的无关业务的组件;并且通过结构图我们很明显的看到struts1是一个基于mvc的框架。




       这篇博客通过一个struts1实例来看struts1框架,先来学会struts1框架是怎样使用的,随后的博客在进一步分析它的实现代码和实现原理。

 

      在写struts1框架实例之前,希望读者能够看一下前面的四篇博客的实例代码,因为那代码是struts1框架的雏形,只要那四篇的代码看懂,那么下面的struts1框架代码也是非常容易看懂的。


Struts1框架实例—登录实例:

1、实例开始工作—导入jar包,在官网上下载struts1框架包,解压之后导入工程的:



      2、之后配置web.xml(这里的具体配置方法可以参见struts1框架包中的实例文件夹webapps中的实例代码中web.xml文件的配置方法):

      

     具体如下:

     

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.     
  11.   <servlet>  
  12.     <servlet-name>action</servlet-name>  
  13.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  14.     <init-param>  
  15.       <param-name>config</param-name>  
  16.       <param-value>/WEB-INF/struts-config.xml</param-value>  
  17.     </init-param>  
  18.     <init-param>  
  19.       <param-name>debug</param-name>  
  20.       <param-value>2</param-value>  
  21.     </init-param>  
  22.     <init-param>  
  23.       <param-name>detail</param-name>  
  24.       <param-value>2</param-value>  
  25.     </init-param>  
  26.     <load-on-startup>2</load-on-startup>  
  27.   </servlet>  
  28.   
  29.   
  30.   <!-- Standard Action Servlet Mapping -->  
  31.   <servlet-mapping>  
  32.     <servlet-name>action</servlet-name>  
  33.     <url-pattern>*.do</url-pattern>  
  34.   </servlet-mapping>    
  35. </web-app></span>  

 

       首先这个配置文件中最主要的就是做了两件的事情,一个是配置ActionServlet,一个是初始化struts-config.xml配置文件参数。如果看了我原先的四篇博客的读者一下子就能想到原先博客中的testServlet和struts-config配置文件。其实这里的ActionServlet还有struts-config和前四篇博客中testservlet、struts-config起到的是一样的作用(这里仅仅介绍如何使用struts,随后的博客在深入分析)。

 

       3、配置完了web.xml文件,之后我们就要开始进入项目代码阶段了。


       登录页面:

      

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="GB18030"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <form action="login.do" method="post">  
  11.         用户:<input type="text" name="username"><br>  
  12.         密码:<input type="password" name="password"></br>  
  13.         <input type="submit" value="登录">  
  14.     </form>  
  15. </body>  
  16. </html></span>  


       切记那个action后面的路径一定要是.do开头的,因为我们在web.xml中配置的是*.do。这里依旧不介绍为什么随后博客会深入分析。

 

      4、建立两个异常类,一个是用户名未找到、一个是密码错误:


      ①用户名未找到

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">public class UserNotFoundException extends RuntimeException {  
  2.   
  3.     public UserNotFoundException() {  
  4.         // TODO Auto-generated constructor stub  
  5.     }  
  6.   
  7.     public UserNotFoundException(String message) {  
  8.         super(message);  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.   
  12.     public UserNotFoundException(Throwable cause) {  
  13.         super(cause);  
  14.         // TODO Auto-generated constructor stub  
  15.     }  
  16.   
  17.     public UserNotFoundException(String message, Throwable cause) {  
  18.         super(message, cause);  
  19.         // TODO Auto-generated constructor stub  
  20.     }  
  21.   
  22. }</span>  

      ②密码错误

 

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">public class PasswordErrorException extends RuntimeException {  
  2.   
  3.     public PasswordErrorException() {  
  4.         // TODO Auto-generated constructor stub  
  5.     }  
  6.   
  7.     public PasswordErrorException(String message) {  
  8.         super(message);  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.   
  12.     public PasswordErrorException(Throwable cause) {  
  13.         super(cause);  
  14.         // TODO Auto-generated constructor stub  
  15.     }  
  16.   
  17.     public PasswordErrorException(String message, Throwable cause) {  
  18.         super(message, cause);  
  19.         // TODO Auto-generated constructor stub  
  20.     }  
  21.   
  22. }</span>  


        5、业务处理类代码:

        

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">public class UserManager {  
  2.   
  3.     public void login(String username, String password) {  
  4.         if (!"admin".equals(username)) {  
  5.             throw new UserNotFoundException();  
  6.         }  
  7.           
  8.         if (!"admin".equals(password)) {  
  9.             throw new PasswordErrorException();  
  10.         }  
  11.           
  12.     }  
  13. }</span>  

 

       6、建立LoginActionForm类,这个类继承ActionForm类,简单说一下这个类,这个类主要是负责收集表单数据的,在这里一定要注意表单的属性必须和actionForm中的get和set方法的属性一致。这里依旧不深入解释,随后博客都会涉及到。

       

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">import org.apache.struts.action.ActionForm;  
  2.   
  3. /** 
  4.  * 登录ActionForm,负责表单收集数据 
  5.  * 表单的属性必须和ActionForm中的get和set的属性一致 
  6.  * @author Administrator 
  7.  * 
  8.  */  
  9. @SuppressWarnings("serial")  
  10. public class LoginActionForm extends ActionForm {  
  11.       
  12.     private String username;  
  13.       
  14.     private String password;  
  15.   
  16.     public String getUsername() {  
  17.         return username;  
  18.     }  
  19.   
  20.     public void setUsername(String username) {  
  21.         this.username = username;  
  22.     }  
  23.   
  24.     public String getPassword() {  
  25.         return password;  
  26.     }  
  27.   
  28.     public void setPassword(String password) {  
  29.         this.password = password;  
  30.     }  
  31.       
  32.       
  33.       
  34. }</span>  


       7、LoginAction类的建立,这个是负责取得表单数据、调用业务逻辑以及返回转向信息。

        

[java]  view plain  copy
 print ?
  1. <span style="font-size:18px;">import javax.servlet.http.HttpServletRequest;  
  2. import javax.servlet.http.HttpServletResponse;  
  3.   
  4. import org.apache.struts.action.Action;  
  5. import org.apache.struts.action.ActionForm;  
  6. import org.apache.struts.action.ActionForward;  
  7. import org.apache.struts.action.ActionMapping;  
  8.   
  9. /** 
  10.  * 登录Action 
  11.  * 负责取得表单数据、调用业务逻辑、返回转向信息 
  12.  *  
  13.  * @author Administrator 
  14.  * 
  15.  */  
  16. public class LoginAction extends Action {  
  17.   
  18.     @Override  
  19.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  20.             HttpServletRequest request, HttpServletResponse response)  
  21.             throws Exception {  
  22.   
  23.           
  24.         LoginActionForm laf=(LoginActionForm)form;  
  25.         String username=laf.getUsername();  
  26.         String password=laf.getPassword();  
  27.         UserManager userManager=new UserManager();  
  28.         try{  
  29.             userManager.login(username, password);  
  30.             return mapping.findForward("success");  
  31.         }catch(UserNotFoundException e){  
  32.             e.printStackTrace();  
  33.             request.setAttribute("msg""用户名不能找到,用户名称=["+username+"]");  
  34.         }catch(PasswordErrorException e){  
  35.             e.printStackTrace();  
  36.             request.setAttribute("msg""密码错误");  
  37.         }  
  38.         return mapping.findForward("error");  
  39.   
  40.      }  
  41. }</span>  


      8、既然有转向,那么我们还要建立两个页面,一个是登录成功页面,一个登录失败页面。

           ①登录成功页面

            

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="GB18030"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     ${loginForm.username },登录成功  
  11. </body>  
  12. </html></span>  


           ②登录失败页面

            

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"  
  2.     pageEncoding="GB18030"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=GB18030">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <%--    
  11.     <%=request.getAttribute("msg") %>  
  12.     --%>  
  13.     ${msg }  
  14. </body>  
  15. </html></span>  

 

       9、最后要进行struts-config.xml的配置,看过原来博客的童鞋,一定对struts-config.xml配置印象深刻,原先博客中的struts-config配置文件是笔者自己写的,用dom4j读取的。而现在struts1中的这个配置文件就要用身价的标签了。而且struts1自己封装了它的读取过程,这样大大减少了我们的开发工作。

        

[html]  view plain  copy
 print ?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="ISO-8859-1" ?>  
  2.   
  3. <!DOCTYPE struts-config PUBLIC  
  4.           "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"  
  5.           "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">  
  6.   
  7. <struts-config>  
  8.     <form-beans>  
  9.         <form-bean name="loginForm" type="com.bjpowernode.struts.LoginActionForm"/>  
  10.     </form-beans>  
  11.       
  12.     <action-mappings>  
  13.         <action path="/login"   
  14.                 type="com.bjpowernode.struts.LoginAction"  
  15.                 name="loginForm"          
  16.                 scope="request"       
  17.                 >  
  18.             <forward name="success" path="/login_success.jsp" />  
  19.             <forward name="error" path="/login_error.jsp"/>         
  20.         </action>  
  21.     </action-mappings>  
  22. </struts-config></span>  


       经过配置之后实例就已经做完了,感兴趣童鞋可以自己手动运行一下。

 

       这篇博客仅仅是提供了一个struts1框架的简单实例,具体分析还希望等下面的博客。在下面的博客中就要对struts1框架进行分析了,我们会看到我们在原先博客mvc实例重构中出现的截取字符串、mapping、读取配置文件的操作是如何在struts1框架中实现的,struts1框架是怎样运行的,它都给我们做了什么等等。敬请期待!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深入浅出STRUTS 2 Struts Ti却发现了二者在技术与开发人员这两个层面上的共同之处,不久之后,两个项目就在WebWork的技术基础上进行了合并2。 当我们说起WebWork的时候,我们实际上说的是两个项目——XWork和WebWork。XWork是一个通用的命令框架,它提供了很多核心的功能,例如actions,验证和拦截器,它可以完全独立于执行上下文运行,并提供了一个内部的依赖注入机制,用来做配置和工厂实现的管理。 而WebWork则是一个完全独立的上下文。它用Web应用中运行所需的上下文把XWork包装起来,并提供了可以简化Web开发的特定实现。 Struts2的目标很简单——使Web开发变得更加容易。为了达成这一目标,Struts2中提供了很多新特性,比如智能的默认设置、annotation的使用以及“惯例重于配置”原则的应用,而这一切都大大减少了XML配置。Struts2中的Action都是POJO,这一方面增强了Action本身的可测试性,另一方面也减小了框架内部的耦合度,而HTML表单中的输入项都被转换成了恰当的类型以供action使用。开发人员还可以通过拦截器(可以自定义拦截器或者使用Struts2提供的拦截器)来对请求进行预处理和后处理,这样一来,处理请求就变得更加模块化,从而进一步减小耦合度。模块化是一个通用的主题——可以通过插件机制来对框架进行扩展;开发人员可以使用自定义的实现来替换掉框架的关键类,从而获得框架本身所不具备的功能;可以用标签来渲染多种主题(包括自定义的主题);Action执行完毕以后,可以有多种结果类型——包括渲染JSP页面,Velocity和Freemarker模板,但并不仅限于这些。最后,依赖注入也成了Struts2王国中的一等公民,这项功能是通过Spring框架的插件和Plexus共同提供的,与PicoContainer的结合工作还正在进行中。 本书的目的,是为了帮助读者掌握Struts2框架,并能够对组成框架的功能部件和可用的配置项有深刻的理解。我在书中还将介绍一些可以提高生产力的方法——包括默认配置项和应当注意的实现特性,可用的多种配置选项和一些开发技术。本书还会就与第三方软件进行集成的话题展开讨论。 2 Don Brown, Struts Ti项目的领导,他在文章中详细介绍了Struts Ti的历史

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值