Struts2 开发流程

长平狐 发布于 2013/01/06 11:34
阅读 1K+
收藏 3

一、确定Struts在开发中的地位

 

 Struts其实就是一个Servlet; 根据传统MVC开发模式,如图所示:

 

 

 

Servlet担任了Controller的职责, 而Struts框架加入后,Struts其实就是担当了Servlet的职责,即任何一个处理请求都会经过Struts框架,并由他进行分发;

 

 

二、Struts2开发须知

 

 

1.在web.xml配置Struts的核心Filter的原因是每个web访问都会经过Struts;

2.struts.xml用于放置struts内部的一些配置信息,即使用Struts的功能,比如配置action、配置逻辑视图和物理视图的联系、配置常量等

3.可以在struts.xml中添加<Constant name="struts.enable.devmode" value="true"/>,表明现在在开发阶段,会提供更多的错误提示供开发人员参考;

4.struts.xml 开发中放置在src目录下,但是部署时放在 WEB-INF\classes目录中(由Eclipse完成);

 


 

核心控制器和业务控制器

 

核心控制器就是在web.xml中配置的StrutsPrepareAndExecutorFilter;

业务控制器就是Action;

 


配置常量

 

常量配置的默认值请看struts2-core-XXXX.jar 中的org/apache/struts2的 default.properties; 该文件中配置了全部的常量的默认值;

常用的常量如下:

 

 常量 默认值 意义
 struts.action.extension  action  指定struts2处理的请求后缀,可以设置多个值,用","分隔
 struts.serve.static.browserCache  true  是否缓存静态内容,如果需要每次都获取最新内容,则设为false
 struts.devMode  false  是否处于开发阶段,即程序出错时给出更多的提示信息
 struts.i18n.encoding  UTF-8  默认编码集,类似于 request.setCharacterEncoding("");
 struts.enable.DynamicMethodInvocation  true  是否支持动态方法调用,即“ActionName ! MethodName”;
 struts.enable.SlashesInActionNames  false  action的名字中是否可以有斜线
 struts.multipart.maxSize  2M  文件上传时的最大上传大小
 struts.multipart.parser

 jakarta

 文件上传的解析器,默认用 commons-fileupload,可以换,但是对编程完全没有影响
 struts.multipart.saveDir  无  文件上传的临时保存路径,几乎不用
 struts.custom.i18n.resources

 testmessages,testmessages2

 设定国际化资源文件的baseName
 struts.ognl.allowStaticMethodAccess

 false

 是否允许ognl调用静态方法

 

这些常量都可以在struts.xml 或 struts.properties、web.xml中进行覆盖; 

 

web.xml中配置常量方法

 

通过配置初始化参数的方式配置常量;

 

[html]  view plain copy
  1. <pre class="html" name="code"><filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4.     <init-param>  
  5.         <param-name>struts.action.extension</param-name>  
  6.         <param-value>do</param-value>  
  7.     </init-param>  
  8. </filter>  
  9. <filter-mapping>  
  10.     <filter-name>struts2</filter-name>  
  11.     <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  
  13.    


 
 

 

Struts2 有 struts.xml 、struts.properties、struts-default.xml、struts-plugin.xml 这四个配置文件;

struts-default.xml struts2-core-Xxx.jar 定义了内建拦截器、默认处理类为ActionSupport
struts-plugin.xml 插件包中 插件的配置文件
struts.xml WEB-INF\classes 定义了action、常量等
struts.properties WEB-INF\classes 配置常量

 

 


 

加载常量的顺序

 

如果出现同一个常量,则后者覆盖前者;

1.struts-default.xml

2.struts-plugin.xml

3.struts.xml

4.struts.properties

5.web.xml

 


 

模块化struts.xml:在struts.xml中加入<include file="struts-1.xml">导入其他配置文件


 

问题:struts.xml没有提示解决方法:

在struts2-core-Xxx.jar 中存在 struts-2.0.dtd;

在Eclipse中操作:

windows-->preference-->xml catalog-->add;

如下图配置:

即可;

三、Struts2简单处理流程

 

 

注意:此处没有涉及拦截器

 

 

接下来我们通过一个开发一个登录处理的web应用,更清晰的说明struts2的流程;

先展示目录结构:

 

一、编写JSP页面

登录页面、登录成功页面、登录失败页面分别为login.jsp,success.jsp,fail.jsp

login.jsp

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  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=utf-8">  
  7. <title>登录界面</title>  
  8. </head>  
  9. <body>  
  10.     <form action="loginAction">  
  11.         用户名:<input type="text" name="user"/><br />  
  12.         密码:<input type="password" name="password"/><br />  
  13.         <input type="submit" value="登录"/>   
  14.     </form>  
  15. </body>  
  16. </html>  

success.jsp

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  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=utf-8">  
  7. <title>登录成功界面</title>  
  8. </head>  
  9. <body>  
  10.     登陆成功!!!  
  11. </body>  
  12. </html>  

fail.jsp

 

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  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=utf-8">  
  7. <title>登录失败界面</title>  
  8. </head>  
  9. <body>  
  10.     登陆失败!!!  
  11. </body>  
  12. </html>  

二、编写Action

自己编写的Action通常需要继承ActionSupport类,我们通常需要覆写 public String execute()throws Exception{}方法;

因为此类规定了一些规范,比如在此类中定义了5个常量: SUCCESS、ERROR、LOGIN、INPUT、NONE;

这5个常量的目的是规定execute方法的返回值;

在Action中可以定义属性,此属性必须要规定setter和getter方法,此属性的用途是保存传递进来的数据;比如此登录应用中Action需要有两个属性:user属性和password属性,用来保存用户名和密码;

LoginAction.java

[java]  view plain copy
  1. package org.login.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class LoginAction extends ActionSupport{  
  6.     private String user;  
  7.     private String password;  
  8.     public String execute()throws Exception{  
  9.         if(user.equals("xiazdong")&&password.equals("12345")){  
  10.             return SUCCESS;  
  11.         }  
  12.         else{  
  13.             return ERROR;  
  14.         }  
  15.     }  
  16.     public String getUser() {  
  17.         return user;  
  18.     }  
  19.     public void setUser(String user) {  
  20.         this.user = user;  
  21.     }  
  22.     public String getPassword() {  
  23.         return password;  
  24.     }  
  25.     public void setPassword(String password) {  
  26.         this.password = password;  
  27.     }  
  28.       
  29. }  

此处没有JavaBean的原因是尽量简化处理过程;

 

三、在struts.xml中配置Action

编写完Action后需要在struts.xml中配置该Action,目的是将execute()方法的返回值(逻辑视图)和物理视图关联起来,简单地说就是确定返回某个值时跳转到某个页面;

 

[html]  view plain copy
  1. <?xml version="1.0" encoding="GBK" ?>  
  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"></constant>  
  8.       
  9.     <package name="MyPackage" extends="struts-default" namespace="/">  
  10.         <action name="loginAction" class="org.login.action.LoginAction">  
  11.             <result name="success">/success.jsp</result>  
  12.             <result name="error">/fail.jsp</result>  
  13.         </action>  
  14.     </package>  
  15. </struts>  

四、处理流程

 

 

 

 

五、改变action后缀

一般我们访问Action类时通过 *.action的方式访问,我们可以修改后缀常量,通过在struts.xml中修改常量的值即可;

<constant name="struts.action.extension" value="do"/>

即可;

如果要支持多个后缀,则可以表示为:

<constant name="struts.action.extension" value="do,action"/>


原文链接: http://blog.csdn.net/sd0902/article/details/8392923
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部