<?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; 注解</title>
	<atom:link href="http://www.mrzeng.com/tag/%e6%b3%a8%e8%a7%a3/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.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>

