`
kyo100900
  • 浏览: 633425 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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 version="1.0" encoding="UTF-8"?>
<web-app>

	<display-name>Struts Blank</display-name>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>

		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>leo.first</param-value>
		</init-param>

	</filter>


	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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

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

 

 

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;
	}
}

 

 

还有它的配置文件:

 

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="first" extends="struts-default" namespace="/first" />
</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下运行即可。

 

分享到:
评论
9 楼 Acaleph 2008-09-10  
去掉@ParentPackage("")的方法就是在Struts.xml中增加一个Constant定义就成了,系统会自动Scan的。
8 楼 itanger 2008-06-17  
顶!顶!顶!顶!顶!顶!
7 楼 jzk 2008-01-16  
就一个文件嘛 看看代码好啦
6 楼 kyo100900 2008-01-15  
通过downpour的提醒,仔细了看了这一章节的文档,总算明白code-behind了。
code-behind在struts2里有两种表现形式:
1.Default mappings (默认映射):其实就是访问那些没有配置过Action的JSP页面,也能像访问Action那样访问。
比如说在项目${root}/leo/a.jsp 有这么一个a.jsp.
我可以在地址栏里输入:http://localhost:8080/项目名称/leo/a.action 来访问这个 a.jsp
效果与 http://localhost:8080/项目名称/leo/a.jsp是一样的。类似于咱们在welcome-file 那里定义的index,
这就是默认映射。

2.Default results (默认结果):其实就是无须显示的在struts-*.xml里配置那些返回 jsp, vm. ftl视图的Action。
比如说,我有一个以下配置文件, 没有result.
<package name="code" extends="struts-default" namespace="/code">
<action name="leo" class="code.LeoAction" />
</package>

Action文件是这样的:
package code;

import com.opensymphony.xwork2.ActionSupport;

public class LeoAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private String flag = "";

public String getFlag() {
return flag;
}

public void setFlag(String flag) {
this.flag = flag;
}

public String execute() {
if (flag == null || flag.equals("")) {
this.addActionMessage("input message");
return INPUT;
} else if (flag != null && flag.equals("error")) {
this.addActionError("error happen");
return ERROR;
} else {
this.addActionMessage("I am leo");
return SUCCESS;
}
}

}

故意设置了一个 flag 来查看结果。 通过,http://localhost:8080/项目名称/leo/leo.action?flag=测试值
你会发现,
INPUT   对应 http://localhost:8080/项目名称/leo/leo-input.jsp
ERROR   对应 http://localhost:8080/项目名称/leo/leo-error.jsp
SUCCESS 对应 http://localhost:8080/项目名称/leo/leo-success.jsp

这就是默认结果的意思。
5 楼 kyo100900 2008-01-14  
downpour 写道
为啥不直接读Struts的Reference?

http://struts.apache.org/2.0.11/docs/codebehind-plugin.html

The Codebehind Plugin reduces mundane configuration by adding "Page Controller" conventions.

第一句话就挑明了来意,整个说明非常清楚。难道人家说的还不够详细?



reference提到的就是 “ To see the plugin in action, review the "Person Manager" example in the Showcase application. “


而我的例子就是从那个 Showcase 中的 Person Manager 剥离出来的, codebehind难道不是这么用吗?
4 楼 downpour 2008-01-14  
为啥不直接读Struts的Reference?

http://struts.apache.org/2.0.11/docs/codebehind-plugin.html

The Codebehind Plugin reduces mundane configuration by adding "Page Controller" conventions.

第一句话就挑明了来意,整个说明非常清楚。难道人家说的还不够详细?
3 楼 kyo100900 2008-01-14  
downpour 写道
code-behind根本不是这么回事。它不解决URL=>Action的转化问题,解决的是Action到View的转化问题。你介绍的是0配置功能。

不过0配置将在2.0.11版本合并到code-behind中。



您能谈谈 code-behind 吗?具体是什么样的? 我在网上找不到例子
2 楼 downpour 2008-01-14  
code-behind根本不是这么回事。它不解决URL=>Action的转化问题,解决的是Action到View的转化问题。你介绍的是0配置功能。

不过0配置将在2.1.0版本合并到code-behind中。
1 楼 jzk 2008-01-14  
@ParentPackage("first")  可以去掉的

相关推荐

Global site tag (gtag.js) - Google Analytics