注意事項:
1.開發(fā)時,應將工程及相應的頁面數(shù)據(jù)庫編碼均設為:utf8解決亂碼問題;
2.當前開發(fā)版本為struts2.1.8,不同的struts2版本會有不同的jar包需要引入,具體見readme.txt
3.autocomplete插件不是很完善,尚不能處理json數(shù)據(jù);彈出層框架只針對圖片(可修改格式適應其它形式)
readme.txt:
- 1.JQuery的Ajax實現(xiàn)并與struts2的結合
- 2.配置過程:
- a.新建web項目;
- b.修改web.xml文件
- c.在src下添加struts.xml文件,添加jar包支持:freemarker.jar ognl.jar struts2-core.jar commons-fileupload.jar
- commons-io.jar xwork-core-2.1.6.jar(這個包加上版本號,是因為下文要提到它),注意jar包支持
- 這六個包是struts必須依賴的jar包
- d.配置jsp文件,在jsp里面添加jquery支持
- e.配置action
- f ( 一)在struts2.1.6之前的版本:
- 添加相應的json包:json-lib.jar jsonplugin.jar 這里要注意很重要的一點,因為json大量引用了Apache commons的包,所以這里要一并加入,
- 需要的commons包共4個,除了commons的包外,還需要引入一個 ezmorph的包,所以這一步一共要引入7個包,列出如下:
- commons-collections.jar commons-lang.jar
- commons-beanutils.jar commons-logging.jar ezmorph.jar 再加上json的兩個包共七個
- (二)struts2.1.6,添加jsonplugin0.34或者jsonplugin0.7
- (三)struts2.1.8則添加json-lib-2.2.3.jar,struts2-json-plugin,struts2-junit-plugin
-
-
- 3.插件使用:
- 1.validation插件:詳見table-validation.jsp,主要為引入一個jquery.validate.js,獲取相應的屬性名來進行驗證;
- 2.tablesort插件:目前用的是兼容中文的官網(wǎng)版本,一是可以實現(xiàn)自動排序,二是支持中文;要注意在table當中的格式,這點很重要
- 3.tablesortpager插件
-
-
-
- JQuery與Json結合的一些注意事項:
- 1.$.get(url,data,function(data){
- eval(data);
- })
- eval(data)將數(shù)據(jù)轉換為js對象;
- 實際上如果返回的json對象,可通過將參數(shù)直接命名為json即可,如下
- $.get(url.data,function(data){},"json")
-
- 2.取json屬性時,盡量用中括號方式,原因是如果有數(shù)字屬性會發(fā)生不識別的現(xiàn)象
- eg:var a=obj["31001"];
- //obj.31001則可能出錯
- 解決辦法有其它方式:比如將屬性值31001改為s31001
-
- 3.亂碼問題:
- 在struts1.2中,通過設置表頭的編碼來解決亂碼問題,注意對于xml和json,要分別設置為text/xml和text/json
- eg:response.setContentType("text/json; charset=utf-8");
1.struts.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- 指定Struts 2配置文件的DTD信息 -->
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.i18n.encoding" value="utf-8"></constant>
- <package name="S2SHJQuery" extends="json-default">
- <action name="show" class="action.ShowAction">
- <result type="json"/>
- </action>
- </package>
- </struts>
2.ShowAction.java配置 :
- package action;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.struts2.json.annotations.JSON;
-
- import com.opensymphony.xwork2.ActionSupport;
- public class ShowAction extends ActionSupport {
- private String testvalue;
- private String result;
- private List<String> list;
- private List<User> userlist;
- private User user;
- private Map<String, User> map;
- private String[] autoarray;
- public String[] getAutoarray() {
- return autoarray;
- }
- public void setAutoarray(String[] autoarray) {
- this.autoarray = autoarray;
- }
- @JSON(serialize=false)
- public Map<String, User> getMap() {
- return map;
- }
- public void setMap(Map<String, User> map) {
- this.map = map;
- }
- @JSON(serialize=false)
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- @JSON(serialize=false)
- public List<User> getUserlist() {
- return userlist;
- }
- public void setUserlist(List<User> userlist) {
- this.userlist = userlist;
- }
- @JSON(serialize=false)
- public List<String> getList() {
- return list;
- }
- public void setList(List<String> list) {
- this.list = list;
- }
-
- public void setTestvalue(String testvalue) {
- this.testvalue = testvalue;
- }
- @JSON(serialize=false)
- public String getTestvalue() {
- return testvalue;
- }
- @JSON(serialize=false)
- public String getResult() {
- return result;
- }
- public void setResult(String result) {
- this.result = result;
- }
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("test is ok: "+testvalue);
- //1.????string
- /*int i=9;
- //result=""+i+"";
- result="中國";*/
- //2.list?
- /*list=new ArrayList<String>();
- list.add("allen");
- list.add("中國");
- list.add("adc");*/
- //3.list?а?User
- /*userlist=new ArrayList<User>();
- User user1=new User();
- user1.setPassword(1);
- user1.setUsername("username1");
- User user2=new User();
- user2.setPassword(2);
- user2.setUsername("username2");
- User user3=new User();
- user3.setPassword(3);
- user3.setUsername("username3");
- userlist.add(user1);
- userlist.add(user2);
- userlist.add(user3);*/
- //4.User???????
- /*user=new User();
- user.setPassword(1);
- String username="中國";
- user.setUsername(username);
- System.out.println(user.getUsername());
- */
- //5.map????
- /*map=new HashMap<String, User>();
- User user1=new User();
- user1.setPassword(1);
- user1.setUsername("username1");
- User user2=new User();
- user2.setPassword(2);
- user2.setUsername("username2");
- User user3=new User();
- user3.setPassword(3);
- user3.setUsername("username3");
- map.put("s1",user1 );
- map.put("s2",user2 );
- map.put("s3",user3 );*/
- /*map=new HashMap<String, String>();//??????String,string???
- map.put("s1","user1");
- map.put("s2","user2");
- map.put("s3","user3");*/
- //7.直接傳遞數(shù)祖
- autoarray=new String[3];
- autoarray[0]="a";
- autoarray[1]="b";
- autoarray[2]="c";
-
- return SUCCESS;
- }
-
-
- }
3.show.jsp配置:
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <mce:script type="text/javascript" src="/S2SHJQuery/js/jquery.js" mce_src="S2SHJQuery/js/jquery.js"></mce:script>
- <base href="<%=basePath%>">
- <title>struts2+jquery+json</title>
- <mce:script type="text/javascript"><!--
- //1.struts2+jquery+json單獨傳遞單個參數(shù)
- /*$(function(){
- $("#jquerytest").click(function(){
- var params={testvalue:$('#test').val()};
- $.ajax({
- url:"show.action",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //data += decodeURI(data.shtml) ;
- alert("成功");
- alert(data.result);
- },
- error: function(){
- alert("失敗");
- }
- })
- })
- })*/
- //2.jquery單獨傳遞含單個參數(shù)的list
- /*$(function(){
- $("#jquerytest").click(function(){
- var params={testvalue:$('#test').val()};
- $.ajax({
- url:"show.action",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- var jsonobject=data.list;
- for(var j=0;j<jsonobject.length;j++)
- {
- alert(jsonobject[j]);
- }
- },
- error: function(){
- alert("失敗");
- }
- })
- })
- })*/
- //3.jquery單獨傳遞含對象類型User的list
- $(function(){
- $("#jquerytest").click(function(){
- var params={testvalue:$('#test').val()};
- $.ajax({
- url:"show.action",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- var jsonobject=data.userlist;
- /*for(var j=0;j<jsonobject.length;j++)
- {
- alert(jsonobject[j].username);
- alert(jsonobject[j].password);
- }
- */
- $.each(jsonobject,function(key,value){
- alert(key+" "+value.username);
- alert(key+" "+value.password);
- })
- }
- ,
- error: function(){
- alert("失敗");
- }
- })
- })
- })
- //4.jquery單獨傳遞User對象類型
- /*$(function(){
- $("#jquerytest").click(function(){
- var params={testvalue:$('#test').val()};
- $.ajax({
- url:"show.action",
- data:params,
- type:'post',
- dataType:'json',
- contentType: "application/x-www-form-urlencoded; charset=UTF-8",
- success:function(data){
- var jsonobject=data.user;
- alert(jsonobject.username);
- },
- error: function(){
- alert("失敗");
- }
- })
- })
- })*/
- //5.jquery傳遞含User對象類型的map類型
- /*$(function(){
- $("#jquerytest").click(function(){
- var params={testvalue:$('#test').val()};
- $.ajax({
- url:"show.action",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- var jsonobject=data.map;
- alert(data.map.s1.username);//一步一步獲取值
- $.each(data.map,function(key,value){
- alert(key+": "+value.username);
- })
- },
- error: function(){
- alert("失敗");
- }
- })
- })
- })*/
- // --></mce:script>
- </head>
- <body>
- <form>
- <input type="button" value="JQuery" id="jquerytest"><br>
- <input type="text" value="美國" name="test" id="test">
- </form>
- </body>
- </html>
4.tableopen.jsp彈出層插件:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
-
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Thickbox Plus - Download by http://www.codefans.net</title>
-
- <mce:style type="text/css" media="all"><!--
- @import "css/global.css";
- --></mce:style><style type="text/css" media="all" mce_bogus="1">@import "css/global.css";</style>
- <mce:script src="/S2SHJQuery/js/jquery.js" mce_src="S2SHJQuery/js/jquery.js" type="text/javascript"></mce:script>
- <mce:script src="/S2SHJQuery/lib/thickbox_plus.js" mce_src="S2SHJQuery/lib/thickbox_plus.js" type="text/javascript"></mce:script>
-
- </head>
- <body>
- <div id="contentPad">
- <h1>Thickbox Plus</h1>
- <div>
- <a href="images/image1.jpg" mce_href="images/image1.jpg" title="Sample caption" class="thickbox"><img src="images/image1_t.jpg" mce_src="images/image1_t.jpg" alt="Image 1" /></a>
-
- <a href="images/image2.jpg" mce_href="images/image2.jpg" title="Another sample caption" class="thickbox"><img src="images/image2_t.jpg" mce_src="images/image2_t.jpg" alt="Image 2"/></a>
- </div>
- </div>
- </body>
- </html>
5.tablesorter.jsp排序層插件:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>列表排序</title>
- <link rel="stylesheet" type="text/css" media="screen"
- href="css/screen.css" />
- <mce:script src="/S2SHJQuery/js/jquery.js" mce_src="S2SHJQuery/js/jquery.js"
- type="text/javascript"></mce:script>
- <mce:script
- src="/S2SHJQuery/lib/jquery.tablesorter.js"
- type="text/javascript"></mce:script>
- <mce:script type="text/javascript"><!--
- $(document).ready(function() {
- $("#userList").tablesorter({sortList: [ [2,1]]} );//也可通過傳遞參數(shù)來實現(xiàn)
- });
-
- // --></mce:script>
- </head>
- <body>
- This is my JSP page.
- <br>
- <table id="userList">
- <thead>
- <tr>
- <th>Name</th>
- <th>Sex</th>
- <th>old</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>李四</td>
- <td>男</td>
- <td>14</td>
- </tr>
- <tr>
- <td>張三</td>
- <td>男</td>
- <td>50</td>
- </tr>
- <tr>
- <td>趙五</td>
- <td>Sex</td>
- <td>60</td>
- </tr>
- <tr>
- <td>麗麗</td>
- <td>女</td>
- <td>18</td>
- </tr>
- <tr>
- <td>蟈蟈</td>
- <td>女</td>
- <td>24</td>
- </tr>
- </tbody>
- </table>
- </body>
- </html>
6.tablesortpager.jsp排序兼分頁插件:
7.tablesuggest.jsp提示插件:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>搜索框提示</title>
- <link rel="stylesheet" type="text/css" media="jquery.autocomplete.css"
- href="css/jquery.autocomplete.css" />
- <mce:script src="/S2SHJQuery/js/jquery.js" mce_src="S2SHJQuery/js/jquery.js"
- type="text/javascript"></mce:script>
- <mce:script
- src="/S2SHJQuery/lib/jquery.autocomplete.js"
- type="text/javascript"></mce:script>
- <mce:script type="text/javascript"><!--
- var arr = ["Allen","Albert","Alberto","Alladin"];
- $(document).ready(function() {
- //$("#userList").autocomplete(arr);
- $("#userList").autocomplete("show.action", { selectFirst:true,extraParams: {testvalue:function(){return $('#userList').val();}},
- width: 170,minChars:1, selectOnly: 1,inputSeparator:':'});
- });
-
- // --></mce:script>
- </head>
- <body>
- This is my JSP page.
- <br>
- <table >
- <tbody>
- <tr>
- <td>提示框測試:<input type="text" name="userList" id="userList"/></td>
- </tr>
- </tbody>
- </table>
- </body>
- </html>
8.table-validation.jsp驗證插件:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" mce_href="css/screen.css" />
- <mce:script src="/S2SHJQuery/js/jquery.js" mce_src="S2SHJQuery/js/jquery.js" type="text/javascript"></mce:script>
- <mce:script src="/S2SHJQuery/lib/jquery.validate.js" mce_src="S2SHJQuery/lib/jquery.validate.js" type="text/javascript"></mce:script>
- <mce:script src="/S2SHJQuery/lib/jquery.validate.messages_cn.js" mce_src="S2SHJQuery/lib/jquery.validate.messages_cn.js" type="text/javascript"></mce:script>
- <mce:style type="text/css"><!--
- * { font-family: Verdana; font-size: 96%; }
- label { width: 10em; float: left; }
- label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
- p { clear: both; }
- .submit { margin-left: 12em; }
- em { font-weight: bold; padding-right: 1em; vertical-align: top; }
- --></mce:style><style type="text/css" mce_bogus="1">* { font-family: Verdana; font-size: 96%; }
- label { width: 10em; float: left; }
- label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
- p { clear: both; }
- .submit { margin-left: 12em; }
- em { font-weight: bold; padding-right: 1em; vertical-align: top; }</style>
- <mce:script type="text/javascript"><!--
- $(document).ready(function(){
- //引入驗證的js.jquery.validate.js,并引入中文js jquery.validate.messages_cn.js
- //可在js里面進行修改,注意css格式
- //獲取form值后,即可根據(jù)id值來進行validation操作
- //取相應的驗證規(guī)則:對應的輸入值即可
- //可參照鋒利的JQuery P218頁
- //同時可利用這一特性生成驗證碼
- $("#commentForm").validate({
- rules: {
- username: {
- required: true,
- minlength: 3
- },
- email: {
- required: true,
- email: true
- },
- url:"url",
- comment: "required"
- }
- });
- });
-
- // --></mce:script>
- </head>
-
- <body>
- <form class="cmxform" id="commentForm" method="get" action="">
- <fieldset>
- <legend>jquery-validation 插件jsp</legend>
- <p>
- <label for="cusername">用戶名:</label>
- <em>*</em><input id="cusername" name="username" size="25" />
- </p>
- <p>
- <label for="cemail">郵件:</label>
- <em>*</em><input id="cemail" name="email" size="25" />
- </p>
- <p>
- <label for="curl">URL:</label>
- <em> </em><input id="curl" name="url" size="25" value="" />
- </p>
- <p>
- <label for="ccomment">評論:</label>
- <em>*</em><textarea id="ccomment" name="comment" cols="22">
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請
點擊舉報。