映射文件
struts.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC </span>
<span style="color: #00bbdd;"> "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"</span>
<span style="color: #00bbdd;"> "http://struts.apache.org/dtds/struts-2.3.dtd"></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><struts<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #808080; font-style: italic;"><!-- 加入i18n.encoding,这里要与传送数据的编码一致 --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"struts.i18n.encoding"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"UTF-8"</span> <span style="color: #000000; font-weight: bold;">/></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"struts.enable.DynamicMethodInvocation"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"false"</span> <span style="color: #000000; font-weight: bold;">/></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"struts.devMode"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"true"</span> <span style="color: #000000; font-weight: bold;">/></span></span>
<span style="color: #808080; font-style: italic;"><!-- Struts 2的Action必须放在指定的包空间下定义 --></span>
<span style="color: #808080; font-style: italic;"><!-- 如果要传送json数据,extends要把原来的struts-default改为json-default --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><package</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"default"</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">"json-default"</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">"/"</span><span style="color: #000000; font-weight: bold;">></span></span>
<span style="color: #808080; font-style: italic;"><!-- 定义login的Action,该Action的实现类为LoginAction类 --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"getJSON"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.stamhe.UserAction"</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">"execute"</span><span style="color: #000000; font-weight: bold;">></span></span>
<span style="color: #808080; font-style: italic;"><!-- 定义处理结果和资源之间映射关系 --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><result</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"json"</span><span style="color: #000000; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;"></result<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></action<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></package<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></struts<span style="color: #000000; font-weight: bold;">></span></span></span></pre></td></tr></table></div>
<p>核心路由文件<br />
web.xml</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00;">?</span></a></sup></span><span class="left2">Download <a href="http://www.stamhe.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=1144&download=web.xml">web.xml</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11442"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p1144code2"><pre class="xml" style="font-family: monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span><span style="color: #000000; font-weight: bold;">?></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><web-app</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"WebApp_9"</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"2.4"</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">"http://java.sun.com/xml/ns/j2ee"</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">"http://www.w3.org/2001/XMLSchema-instance"</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</span><span style="color: #000000; font-weight: bold;">></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><display-name<span style="color: #000000; font-weight: bold;">></span></span></span>Struts Blank<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></display-name<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><filter<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #808080; font-style: italic;"><!-- 定义核心Filter的名字 --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><filter-name<span style="color: #000000; font-weight: bold;">></span></span></span>struts2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></filter-name<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #808080; font-style: italic;"><!-- 定义核心Filter的实现类 --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><filter-class<span style="color: #000000; font-weight: bold;">></span></span></span>org.apache.struts2.dispatcher.FilterDispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></filter-class<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></filter<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #808080; font-style: italic;"><!-- FilterDispatcher用来初始化Struts 2并且处理所有的Web请求 --></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><filter-mapping<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><filter-name<span style="color: #000000; font-weight: bold;">></span></span></span>struts2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></filter-name<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><url-pattern<span style="color: #000000; font-weight: bold;">></span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></url-pattern<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></filter-mapping<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></web-app<span style="color: #000000; font-weight: bold;">></span></span></span></pre></td></tr></table></div>
<p>UserAction.java</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00;">?</span></a></sup></span><span class="left2">Download <a href="http://www.stamhe.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=1144&download=UserAction.java">UserAction.java</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11443"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p1144code3"><pre class="java" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.stamhe</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.UnsupportedEncodingException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.opensymphony.xwork2.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport<span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> dateJSON<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> Sex<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> execute<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">UnsupportedEncodingException</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> java.<span style="color: #006633;">net</span>.<span style="color: #003399;">URLDecoder</span>.<span style="color: #006633;">decode</span><span style="color: #009900;">(</span>name, <span style="color: #0000ff;">"UTF-8"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">(</span><span style="color: #0000ff;">"从客户端接受到的数据: "</span> <span style="color: #339933;">+</span> name<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
dateJSON<span style="color: #339933;">=</span><span style="color: #0000ff;">"[{name: 'stamhe', age: 30}, {name: 'hestam', age: 32}]"</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> SUCCESS<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDateJSON<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">return</span> dateJSON<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDateJSON<span style="color: #009900;">(</span><span style="color: #003399;">String</span> dateJSON<span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dateJSON</span> <span style="color: #339933;">=</span> dateJSON<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">(</span><span style="color: #003399;">String</span> name<span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getSex<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">return</span> Sex<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSex<span style="color: #009900;">(</span><span style="color: #003399;">String</span> Sex<span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">Sex</span> <span style="color: #339933;">=</span> Sex<span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre></td></tr></table></div>
<p>index.jsp</p>
<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00;">?</span></a></sup></span><span class="left2">Download <a href="http://www.stamhe.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=1144&download=index.jsp">index.jsp</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11444"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p1144code4"><pre class="jsp" style="font-family: monospace;"><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=UTF-8">
<title>ajax</title>
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$.ajax({
type: 'POST',
url: 'http://jsp.stamhe.com/test2/getJSON.action',
data: "name=" + encodeURI("蓝海魔方"),
dataType: 'json',
success: function(data){
$("#rdiv").html("lssrc返回的数据:" + data.dateJSON);
}
});
});
});
</script>
</head>
<body>
<input type="button" id="btn" value="按钮" />
<div id="rdiv">要显示数据的区域</div>
</body>
</html></pre></td></tr></table></div>
<hr />
<p><small>© Stam He for <a href="http://www.stamhe.com">Stam He的博客</a>, 2013. |
<a href="http://www.stamhe.com/?p=1144">Permalink</a> |
<a href="http://www.stamhe.com/?p=1144#comments">2 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://www.stamhe.com/?p=1144&title=Struts2 jQuery ajax 示例">del.icio.us</a>
<br />
Post tags: <a href="http://www.stamhe.com/?tag=ajax%e5%ae%9e%e4%be%8b" rel="tag">ajax实例</a>, <a href="http://www.stamhe.com/?tag=struts2" rel="tag">Struts2</a><br />
</small></p>
<p><small>Feed enhanced by <a href="http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/">Better Feed</a> from <a href="http://planetozh.com/blog/">Ozh</a></small></p> |