<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>曾华水的博客 &#187; Annotations</title>
	<atom:link href="http://www.mrzeng.com/tag/annotations/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mrzeng.com</link>
	<description>NO.1 or Nothing</description>
	<lastBuildDate>Wed, 07 Sep 2011 12:47:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Struts 2注解实例二</title>
		<link>http://www.mrzeng.com/post/struts-2-annotations-example-1.html</link>
		<comments>http://www.mrzeng.com/post/struts-2-annotations-example-1.html#comments</comments>
		<pubDate>Thu, 25 Feb 2010 07:53:34 +0000</pubDate>
		<dc:creator>zenghuashui</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Annotations]]></category>
		<category><![CDATA[Struts2]]></category>

		<guid isPermaLink="false">http://www.mrzeng.com/?p=146</guid>
		<description><![CDATA[这个例子将继续前面讲的例子 Here we will see the same hello user example with the following changes. Our Action class ends with the world Action and does not implement com.opensymphony.xwork2.Action. We use /results directory for storing our result pages instead of WEB-INF/content. 和上个例子对比，我们做了如下变更， Action类以Action结尾，同时不实现com.opensymphony.xwork2.Action接口 我们使用/results目录为存放我们的result页面，而不再放在WEB-INF/content目录. The directory structure of the hello user example is shown below. 目录的结构如下 Our [...]]]></description>
			<content:encoded><![CDATA[<p>这个例子将继续<a href="http://huashui.org/post/struts-2-annotations-example.html">前面讲的例子</a><br />
<span id="more-146"></span><br />
Here we will see the same hello user example with the following changes.</p>
<ul>
<li>Our Action class ends with the world Action and does not implement com.opensymphony.xwork2.Action. </li>
<li>We use /results directory for storing our result pages instead of WEB-INF/content. </li>
</ul>
<p>和上个例子对比，我们做了如下变更，</p>
<ul>
<li>Action类以Action结尾，同时不实现com.opensymphony.xwork2.Action接口</li>
<li>我们使用/results目录为存放我们的result页面，而不再放在WEB-INF/content目录.</li>
</ul>
<p>The directory structure of the hello user example is shown below.<br />
目录的结构如下<br />
<div class="wp-caption alignnone" style="width: 305px"><a href="http://www.vaannila.com/images/struts2/Annotation2Pic1.gif"><img alt="Annotation2Pic1" src="http://www.vaannila.com/images/struts2/Annotation2Pic1.gif" title="Annotation2Pic1" width="295" height="545" /></a><p class="wp-caption-text">Annotation2Pic1</p></div></p>
<p>Our WelcomeUserAction class is a simple pojo class. The important thing to note here is that our Action classs name ends with the word Action.<br />
我们的WelcomeUserAction类是一个简单的pojo类。但重要的是我们的类名药以Action结尾。</p>
<pre class=prettyprint>
package com.vaannila.action; 

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result; 

public class WelcomeUserAction { 

	private String userName; 

	private String message; 

	@Action(value="/welcome",results={ @Result(name="success",location="/results/successPage.jsp")}) 

	public String execute() { 

		message = "Welcome " + userName + " !"; 

		return "success"; 

	} 

	public void setUserName(String userName) { 

		this.userName = userName; 

	} 

	public void setMessage(String message) { 

		this.message = message; 

	} 

	public String getUserName() { 

		return userName; 

	} 

	public String getMessage() { 

		return message; 

	}
}
</pre>
<p>Here we use Action and Result annotations just to show you how to use them, for simple example like this you can use the intelligent defaults provided by the Convention plug-in.<br />
这里，我们使用Action和Result注解主要是为了来介绍如何使用它们，我们也可以想前一个简单例子那样使用插件包默认的方式。</p>
<p>The Convention plug-in uses the Action class name to map the action URL. The Convention plug-in first removes the world Action at the end of the class name and then converts the camel case name to dashes. So by default our WelcomeUserAction will be invoked for the request URL welcome-user. But if you want the Action to be invoked for a different URL then you can do this by using the <strong>Action annotation</strong>.<br />
Convention 插件使用Actin类名来映射器URL。它首先去掉类后面的Action后缀，然后在使用驼峰的地方转换为破折号。所以，默认情况下WelcomeUserAction要通过请求welcome-user来访问。但如果你要让Action使用一个不同的URL来进行，你必须使用<strong>Action注解</strong>。</p>
<p>The value of the Action annotation is “/welcome”, this means that the action will be invoked for the request URL “/welcome”. You can change the default action and URL mapping using the Action annotation.<br />
Action注解的值为“/welcome”，这意味着这个action将通过/welcome进行访问。你能够通过使用Action注解来改变默认的action和URL映射。</p>
<p>Now based on the result code from action the Convention plug-in will look for the result name welcome-resultcode in the directory WEB-INF/content. You can change this to a different location by setting the property <em>struts.convention.result.path </em>to a new value in the Struts properties file. In this example we store the result pages in /results directory.<br />
现在，在Action的result code基础上，Convention将在WEB-INF/content查找welcome-resultcode的文件。有可以通过改变struts的properties文件中的<em>struts.convention.result.path </em>值来改变results的目录。</p>
<pre class=prettyprint>
struts.properties file
-----------------------
struts.convention.result.path=/results
</pre>
<p>Our result page name is successPage.jsp, the Convention plug-in will look for a page like welcome.jsp ( the file can even be a freemaker or velocity file ) since our URL is “/welcome”. In this case it will give an error, if we are not specifying which result it should invoke when the result is “success”. To do this we use the Result annotation.<br />
我们的result页面的名字是successPage.jsp。Convention插件会查找welcome.jsp文件，因为我们Action的URL为“/welcome”。在这个例子中，如我们没有指定他的的success的result，那么将会出错。为此，我们可以使用Result注解。</p>
<p>The <strong>Result annotation </strong>maps the result code with the result page. Here the result code “success” is mapped to the result “/results/successPage.jsp”.</p>
<p>在Result注解中，我们用一个result code来指定映射的页面。这里的result code为“success”，被映射到的“/results/successPage.jsp”页面。</p>
<p>The annotations needs to be specified only when you are not using the default naming conventions, if you use them you can keep writing action classes and result pages without any configuration and the framework know exactly when to invoke them.<br />
当你没有使用插件默认的名字规约的时候来需要使用注解。如果你使用了，你可以实现没有任何配置文件下些Action和result页面，而框架能够根据需要进行调用。</p>
<p>源码<br />
Source :<a href="http://www.vaannila.com/examples/struts2/src/Struts2Example2.zip">Download </a><br />
Source + Lib :<a href="http://www.vaannila.com/examples/struts2/example/Struts2Example2.war">Download</a> </p>
<p>原文地址：<a href="http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotations-example-1.html">http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotations-example-1.html</a></p>
<h3  class="related_post_title">相关日志</h3><ul class="related_post"><li><a href="http://www.mrzeng.com/post/struts2-action-request-response-session.html" title="struts2的action中获得request response session 对象">struts2的action中获得request response session 对象</a><br /><small>在struts2中有两种方式可以得到这些对象,一般有两种方式，一种是利用Ioc实现拦截器接口，一种是使用静态类。分布如下

第一种方式，非IoC方式，使用ActionContext类和ServletActionContext类，例如

HttpServletResponse response(HttpServletResponse) =
ActionContext.getContext(...</small></li><li><a href="http://www.mrzeng.com/post/struts-2-annotations-example.html" title="Struts 2注解实例一">Struts 2注解实例一</a><br /><small>本文将通过一个前台通过表单输入user，来实现hello user的方式来讲述Struts2中使用注解。

首先要引入以下jar包

commons-fileupload-1.2.1 
commons-io-1.3.2 
commons-logging-1.1 
freemarker-2.3.13 
junit-3.8.1 
onl-2.6.11 
spring-test-2....</small></li><li><a href="http://www.mrzeng.com/post/struts2-resources-study.html" title="Struts2的教程资源下载汇总">Struts2的教程资源下载汇总</a><br /><small>目前，国内的java培训貌似很热，这应该和国内的环境有关系。特别是随着电子商务的发展，越来越多的平台都转换到java的平台上来进行开发建设。
原来自己一直使用Struts1，虽然很早知道Struts2出来了，但是一直没时间去看。最近花了点时间看下，感觉确实比Struts1.x好得多。个人感觉有以下几点
   1、可以开始使用注解，使用注解的方式一直是我最喜欢的开发方式。虽然从软件开发的理念来讲...</small></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrzeng.com/post/struts-2-annotations-example-1.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Struts 2注解实例一</title>
		<link>http://www.mrzeng.com/post/struts-2-annotations-example.html</link>
		<comments>http://www.mrzeng.com/post/struts-2-annotations-example.html#comments</comments>
		<pubDate>Thu, 25 Feb 2010 07:23:53 +0000</pubDate>
		<dc:creator>zenghuashui</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Annotations]]></category>
		<category><![CDATA[Struts]]></category>
		<category><![CDATA[注解]]></category>

		<guid isPermaLink="false">http://www.mrzeng.com/?p=142</guid>
		<description><![CDATA[本文将通过一个前台通过表单输入user，来实现hello user的方式来讲述Struts2中使用注解。 首先要引入以下jar包 commons-fileupload-1.2.1 commons-io-1.3.2 commons-logging-1.1 freemarker-2.3.13 junit-3.8.1 onl-2.6.11 spring-test-2.5.6 struts2-convention-plugin-2.1.6 struts2-core-2.1.6 xwork-2.1.2 首先要确定一下版本是否正确，此外struts2-convention-plugin-2.1.6 是必须加入的，这个包是用来注解的核心包。 目录的结构如下： 接下来书写表单输入页面 &#60; %@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&#62; &#60; !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&#62; &#60; %@taglib uri="/struts-tags" prefix="s" %&#62; &#60;html&#62; &#60;head&#62; &#60;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&#62; &#60;title&#62;Hello World&#60;/title&#62; &#60;/meta&#62;&#60;/head&#62; &#60;body&#62; &#60;s:form action="welcome-user" &#62; &#60;s:textfield name="userName" label="User Name" [...]]]></description>
			<content:encoded><![CDATA[<p>本文将通过一个前台通过表单输入user，来实现hello user的方式来讲述Struts2中使用注解。<br />
<span id="more-142"></span><br />
首先要引入以下jar包</p>
<pre class=prettyprint>
commons-fileupload-1.2.1
commons-io-1.3.2
commons-logging-1.1
freemarker-2.3.13
junit-3.8.1
onl-2.6.11
spring-test-2.5.6
struts2-convention-plugin-2.1.6
struts2-core-2.1.6
xwork-2.1.2
</pre>
<p>首先要确定一下版本是否正确，此外struts2-convention-plugin-2.1.6 是必须加入的，这个包是用来注解的核心包。<br />
目录的结构如下：<br />
<div class="wp-caption alignnone" style="width: 327px"><a href="http://www.vaannila.com/images/struts2/Annotation1Pic1.gif"><img alt="structure of the hello user example" src="http://www.vaannila.com/images/struts2/Annotation1Pic1.gif" title="structure of the hello user example" width="317" height="514" /></a><p class="wp-caption-text">structure of the hello user example</p></div><br />
接下来书写表单输入页面</p>
<pre class=prettyprint>
&lt; %@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
&lt; !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt; %@taglib uri="/struts-tags" prefix="s" %&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
		&lt;title&gt;Hello World&lt;/title&gt;
	&lt;/meta&gt;&lt;/head&gt;
	&lt;body&gt;
		&lt;s:form action="welcome-user" &gt;
		&lt;s:textfield name="userName" label="User Name" /&gt;
		&lt;s:submit /&gt;
		&lt;/s:form&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>需要注意的是表单中action为url的名字。<br />
我们在WelcomeUser 的类中布置好欢迎信息，返回“success”。</p>
<pre class=prettyprint>
package com.vaannila.action; 

import com.opensymphony.xwork2.ActionSupport; 

public class WelcomeUser extends ActionSupport{ 

	private String userName; 

	private String message; 

	public String execute() { 

		message = "Welcome " + userName; 

		return SUCCESS; 

	} 

	public void setUserName(String userName) { 

		this.userName = userName; 

	} 

	public void setMessage(String message) { 

		this.message = message; 

	} 

	public String getUserName() { 

		return userName; 

	} 

	public String getMessage() { 

		return message; 

	}
}
</pre>
<p>这里有没有发现我们action的url和名字有没有相似之处呢？<br />
我们再welcome-user.jsp 显示返回的欢迎信息。如下：</p>
<pre class=prettyprint>
&lt; %@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%&gt;
&lt; !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
		&lt;title&gt;Welcome User&lt;/title&gt;
	&lt;/meta&gt;&lt;/head&gt;
	&lt;body&gt; 

		&lt;h1&gt;${message}&lt;/h1&gt; 

	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>配置web.xml</p>
<pre class=prettyprint>
&lt; ?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web -app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt; 

&lt;display -name&gt;Struts2Example1&lt;/display&gt; 

&lt;filter&gt; 

	&lt;/filter&gt;&lt;filter -name&gt;struts2&lt;/filter&gt; 

	&lt;filter -class&gt; 

	org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter 

	&lt;/filter&gt; 

&lt;filter -mapping&gt; 

	&lt;/filter&gt;&lt;filter -name&gt;struts2&lt;/filter&gt; 

	&lt;url -pattern&gt;/*&lt;/url&gt; 

&lt;welcome -file-list&gt; 

	&lt;/welcome&gt;&lt;welcome -file&gt;index.jsp&lt;/welcome&gt; 

&lt;/web&gt;
</pre>
<p>部署执行下http://localhost:8080/Struts2Example1，我们可以看到。<br />
<div id="attachment_297" class="wp-caption alignnone" style="width: 310px"><a href="http://huashui.org/wp-content/uploads/2010/02/Annotation1Pic2.gif"><img src="http://huashui.org/wp-content/uploads/2010/02/Annotation1Pic2-300x138.gif" alt="Annotation1Pic2" title="Annotation1Pic2" width="300" height="138" class="size-medium wp-image-297" /></a><p class="wp-caption-text">Annotation1Pic2</p></div><br />
随便输入一个名字，即可得到如下<br />
<div id="attachment_298" class="wp-caption alignnone" style="width: 310px"><a href="http://huashui.org/wp-content/uploads/2010/02/Annotation1Pic3.gif"><img src="http://huashui.org/wp-content/uploads/2010/02/Annotation1Pic3-300x138.gif" alt="Annotation1Pic3" title="Annotation1Pic3" width="300" height="138" class="size-medium wp-image-298" /></a><p class="wp-caption-text">Annotation1Pic3</p></div></p>
<p>接下来，我们看下它是如何进行的。<br />
<strong>Convention plug-in</strong>是在后台运行的。</p>
<ul>
<li>considered as the root package for the Convention plug-in.<br />
默认下，Convention plug-in 会在struts，struts2，action或者actions等包中进行查找。这里，我们的包名为com.vaannila.action。任何满足以上条件的的包都将被引入。</li>
<li>action类应该要继承com.opensymphony.xwork2.Action接口或者action类名以Action结尾。我们的例子中，WelcomeUser继承于com.opensymphony.xwork2.ActionSupport，从而也是实现了com.opensymphony.xwork2.Action。</li>
<li>Convention plug-in使用action的类名来映射成对应的URL。例子中的类名为WelcomeUser，它对应的URL为welcome-user.</li>
<li>接下里看看下results，他并不知道文件在哪个目录下面，但Convention plug-in将根据Action名来进行判断，例如，如何返回“success”，那么将自动查找文件名为welcome-user-success.jsp 或 welcome-user.jsp。</li>
</ul>
<p>Source :<a href="http://www.vaannila.com/examples/struts2/src/Struts2Example1.zip">Download </a><br />
Source + Lib :<a href="http://www.vaannila.com/examples/struts2/example/Struts2Example1.war">Download</a>  </p>
<p>原文地址：<br />
<a href="http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotation-example-1.html">http://www.vaannila.com/struts-2/struts-2-example/struts-2-annotation-example-1.html</a></p>
<h3  class="related_post_title">相关日志</h3><ul class="related_post"><li><a href="http://www.mrzeng.com/post/struts-2-annotations-example-1.html" title="Struts 2注解实例二">Struts 2注解实例二</a><br /><small>这个例子将继续前面讲的例子

Here we will see the same hello user example with the following changes.


	Our Action class ends with the world Action and does not implement com.opensymphony.xwork2.Action. 
	We...</small></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.mrzeng.com/post/struts-2-annotations-example.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

