2012年12月26日

第一part Struts 2应用程序开发基本流程

简单的流程描述如下:页面->控制器->页面

Struts 2的基本简要流程如下:

  1.   客户端浏览器发出HTTP请求。
  2. 根据web.xml配置,该请求被过滤器FilterDispatcher接收并处理。
  3.   过滤器处理完成后继续将请求交给拦截器处理,拦截器将完成一些通用功能操作,例如表单验证等。
  4. 拦截器处理完后,根据struts.xml配置,找到需要调用的Action类和方法,把请求交给Action处理,默认情况下执行Actionexcute方法,该方法可以访问业务逻辑层完成对数据库的访问。
  5. 最后由excute方法返回的结果字符串,根据struts.xml配置决定返回什么样的页面或结果到浏览器。

 

如何利用Struts 2框架开发一个简单的应用程序?

步骤如下:

1)      新建web项目
2)      为项目导入struts2的核心类库
3)      在web.xml文件中配置过滤器FilterDispatcher
4)      编写JSP页面
5)      编写Action类,重载类中的excute方法
6)      新建struts.xml文件,配置action及其result信息。
7)      把项目发布到服务器,启动tomcat,运行调试

 

 

项目需要导入的核心类型有以下9个:

 

Struts 2 框架学习 第1part 基本开发流程_框架

 

 

web.xml文件的配置如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
  7.   <display-name></display-name>   
  8.   <welcome-file-list> 
  9.     <welcome-file>index.jsp</welcome-file> 
  10.   </welcome-file-list> 
  11.   <filter> 
  12.     <filter-name>struts 2</filter-name> 
  13.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
  14.   </filter> 
  15.   <filter-mapping> 
  16.     <filter-name>struts 2</filter-name> 
  17.     <url-pattern>/*</url-pattern> 
  18.   </filter-mapping> 
  19. </web-app> 

 

 

最简单的struts.xml配置如下:

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.  
  6. <struts> 
  7.      <constant name="struts.devMode" value="true" /> 
  8.      <package name="default" namespace="/" extends="struts-default"> 
  9.           
  10.         <action name="hell"> 
  11.             <result> 
  12.                 /Hello.jsp  
  13.             </result> 
  14.         </action> 
  15.     </package> 
  16.     <!-- Add packages here --> 
  17.  
  18. </struts>