Liny_@NotePad

沉迷ACG中

Struts2零配置

YOYO posted @ 2009年4月25日 00:05 in 【Java EE】 with tags struts2 零配置 , 5091 阅读

百度了下,貌似有使用Annotations和CoC两种。
但它们并不是真正的零配置,只是在一定情况下简化了。
我还是会比较喜欢原版的 = = 不过蛮写个demo看看。

首先是使用Annotations

这就是jdk1.5开始提供的注解语法,与以往javadoc生成doc时所用的那些@author\@version等不同,拥有更强大的功能。
这里就不具体表述,只贴一个使用struts2中的result注解的demo,详细在网上可以找到很多资料。

首先还是web.xml:

  1. <?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>login.action</welcome-file>
  9.   </welcome-file-list>
  10.  
  11.   <filter>
  12.         <filter-name>struts2</filter-name>
  13.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  14.         <init-param>
  15.               <param-name>actionPackages</param-name>
  16.               <param-value>web.action, test.action</param-value>
  17.         </init-param>
  18.   </filter>
  19.  
  20.   <filter-mapping>
  21.         <filter-name>struts2</filter-name>
  22.         <url-pattern>/*</url-pattern>
  23.   </filter-mapping>
  24.  
  25. </web-app>

 

注意这里配struts2的filter时配上的init-param,
参数actionPackages表示放action的包,多个包之前用英文逗号分隔。
当你要访问action时,就直接访问类名(去掉action).action即可。

这里就不需要struts.xml来配置action了,那result怎么返回呢?且先看一个Action。

web.action.LoginAction

  1. package web.action;
  2.  
  3. import org.apache.struts2.config.NullResult;
  4. import org.apache.struts2.config.Result;
  5. import org.apache.struts2.config.Results;
  6.  
  7. @Results({
  8.         @Result(name="success", type=NullResult.class, value="/success.jsp"),
  9.         @Result(name="fail", type=NullResult.class, value="/fail.jsp")
  10. })
  11. public class LoginAction {
  12.  
  13.         public String execute(){
  14.                 return "fail";
  15.         }
  16.        
  17.         public String login(){
  18.                 return "success";
  19.         }
  20.        
  21. }

使用@Results可以表示它的结果集,各个可能的结果就在里面用一个个@Result来表示。
name就是return的内容,type一般是NullResult.class,还有很多其他种就是了 = =。value则是跳转的页面地址。

这样就完成了一个基本的acton,下面再写一个
test.action.TestAction

  1. package test.action;
  2.  
  3. import org.apache.struts2.config.NullResult;
  4. import org.apache.struts2.config.Result;
  5. import org.apache.struts2.config.Results;
  6.  
  7. @Results({
  8.         @Result(name="success", type=NullResult.class, value="/success.jsp")
  9. })
  10. public class TestAction {
  11.        
  12.         public String execute(){
  13.                 return "success";
  14.         }
  15.  
  16. }

它只有一个返回值。

现在我们随便做两个jsp,表示success和fail:

success.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.   <head>
  5.     <title>Struts2 Zero Configuration</title>
  6.   </head>
  7.  
  8.   <body>
  9.     <h3>Success</h3>
  10.   </body>
  11. </html>

 

fail.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4.   <head>
  5.     <title>Struts2 Zero Configuration</title>
  6.   </head>
  7.  
  8.   <body>
  9.     <h3>Fail</h3>
  10.   </body>
  11. </html>

这时访问login.action,应该会跳转到Fail,访问login!login.action才能访问到success。

而访问test.action,就直接跳到success了。

这就完成了零配置。其实还是比较麻烦,因为它要对所有action都使用result。

而使用CoC,就不需要在各个Action中都写,

它的web.xml和annontations的配置一样,
但它还需要配置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.         <package name="default-package" extends="struts-default">   
  8.                 <global-results>
  9.                         <result>/success.jsp</result>
  10.                         <result name="fail">/fail.jsp</result>
  11.                 </global-results>
  12.         </package>
  13.        
  14.         <constant name="struts.configuration.classpath.defaultParentPackage" value="default-package" />
  15. </struts>

它用的是global-rsults,全局的。
因此所有action返回的result都会到这里,找到相应的映射跳出去。之前的那两个Action类就不需要任何注释了~

效果和annontations一样。这个比较简单 = = 但是好像不同action返回相同的结果那跳转的页面就都一样了 还是感觉不爱。。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter