Struts2的第一个入门实例(二)--什么是code-behind

Struts2的Code-behind究竟是什么?ROR那样的COC配置风格吗?我在论坛里找不到关于Struts2的code-behind确切的实例,只有那个发布包中隐隐约约有一个关于person操作采用的就是code-behind风格,那么code-behind是否真的适合你?我们现在来看一个最简单的code-behind入门实例。
开发环境为:XP2下的Struts2.0.11版本, 先将所有的jar包都放入到classpath下,注意struts2-codebehind-plugin-2.0.11.jar 这个包不能少,否则code-behind无法正常使用。

打开web.xml文件,配置下:

 

Xml代码
<?xml version="1.0" encoding="UTF-8"?>  
  1. <web-app>  
  2.   
  3.     <display-name>Struts Blank</display-name>  
  4.   
  5.     <filter>  
  6.         <filter-name>struts2</filter-name>  
  7.         <filter-class>  
  8.             org.apache.struts2.dispatcher.FilterDispatcher   
  9.         </filter-class>  
  10.   
  11.         <init-param>  
  12.             <param-name>actionPackages</param-name>  
  13.             <param-value>leo.first</param-value>  
  14.         </init-param>  
  15.   
  16.     </filter>  
  17.   
  18.   
  19.     <filter-mapping>  
  20.         <filter-name>struts2</filter-name>  
  21.         <url-pattern>/*</url-pattern>  
  22.     </filter-mapping>  
  23.   
  24.   
  25.     <welcome-file-list>  
  26.         <welcome-file>index.jsp</welcome-file>  
  27.     </welcome-file-list>  
  28. </web-app>  

这里需要注意的就是在配置Struts2的时候,多了一个actionPackages,表示code-behind会去搜索指定包下的Action类,(文档提到,struts.properties文件也可以设置,但我没有成功过。) 在我这里指定的是 leo.first包下的Action类。

然后来一个简单的Action,CoC风格:

 

 

Java代码
package leo.first;   
  1.   
  2. import org.apache.struts2.config.ParentPackage;   
  3.   
  4. import com.opensymphony.xwork2.ActionSupport;   
  5.   
  6. @ParentPackage("first")   
  7. public class FirstAction extends ActionSupport {   
  8.   
  9.     private String name;   
  10.   
  11.     public String getName() {   
  12.         return name;   
  13.     }   
  14.   
  15.     public void setName(String name) {   
  16.         this.name = name;   
  17.     }   
  18.   
  19.     public String execute() {   
  20.         name = "superleo";   
  21.         return SUCCESS;   
  22.     }   
  23. }  
package leo.first;

import org.apache.struts2.config.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("first")
public class FirstAction extends ActionSupport {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String execute() {
		name = "superleo";
		return SUCCESS;
	}
}

 

 

还有它的配置文件:

 

Xml代码
<!DOCTYPE struts PUBLIC   
  1.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"   
  2.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  3.   
  4. <struts>  
  5.     <package name="first" extends="struts-default" namespace="/first" />  
  6. </struts>  

 

 

FirstAction 里的@ParentPackage("first")对应的就是它配置文件里的package name="first", 也就是说想实现一个code-behind并不能真正“零配置”,与ROR的COC还是差距不小的。完成所有配置后,可以运行代码了,在你的地址栏里输入:http://localhost:8080/code_behind/first/first.action 相关的action就能正常执行了。从头到尾发现只有在配置文件里,配置那些action的url工作少了,其它的还是不变,而且Action还需要使用元数据,因此感觉是XML+Annotation勉强组合在一起。 不知道大家在使用code-behind是怎么简化开发的呢?

 

 

源程序在附件里,大家感兴趣的话,可以下载看看,直接导入到MyEclipse下运行即可。

 

http://superleo.iteye.com/blog/155425

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值