下载 jsonplugin , jQuery
从网址http://code.google.com/p/jsonplugin/downloads/list 下载JSON插件的JAR包。
从网址http://docs.jquery.com/Downloading_jQuery 下载jquery所需文件。(建议下载稳定版本,不然会出现莫名其妙的错误)
1.建立工程。添加JAR包 到 WEB-INF / lib 下。
Struts2 : struts2-core-[version].jar
xwork-[version].jar
ognl-[version].jar
freemarker-[version].jar
commons-logging-[version].jar
jsonplugin: jsonplugin-[version].jar
jQuery: jquery-[version].min.js
2.配置 struts2
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </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>
3.业务实现 AJAX 查询 所有的 BOOK 对象。 在页面已 SELECT 显示.
Model
Book.java
package com.wei.model; /*** * * @author wei * */ public class Book { private int bookId; private String bookName; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } }
Service
BookService.java
package com.wei.service; import java.util.ArrayList; import java.util.List; import com.wei.model.Book; /*** * * @author wei * */ public class BookService { /** * 返回书本对象集合 * * @return List<Book> */ public static List<Book> getBook() { List<Book> list = new ArrayList<Book>(); // 添加测试数据 for (int i = 1; i <= 10; i++) { Book o = new Book(); o.setBookId(i); o.setBookName("test"); list.add(o); } return list; } }
Action
BookAction.java
package com.wei.action; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.wei.model.Book; import com.wei.service.BookService; /*** * * @author wei * */ public class BookAction extends ActionSupport { private String strAjaxChannel = null; public String getStrAjaxChannel() { return strAjaxChannel; } public void setStrAjaxChannel(String strAjaxChannel) { this.strAjaxChannel = strAjaxChannel; } @Override public String execute() throws Exception { StringBuffer sb = new StringBuffer(); List<Book> list = BookService.getBook(); if (list.size() > 0) { int j = list.size(); sb.append("{"); for (int i = 0; i < list.size(); i++) { Book b = list.get(i); sb.append(b.getBookId()); sb.append(":"); sb.append("/""); sb.append(b.getBookName()); sb.append("/""); if (i != (j - 1)) sb.append(","); } sb.append("}"); } strAjaxChannel = sb.toString(); return SUCCESS; } }
注意: extends ActionSupport 不然可能报错。
4.配置Action
在 SRC 下建立 struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="sajax" extends="json-default"> <action name="getBook" class="com.wei.action.BookAction"> <result type="json"></result> </action> </package> </struts>
注意: 1. package extends="json-default" jsonplugin 里的默认配置
2. action <result type="json"></result> 返回类型 json
5.视图
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <mce:script type="text/javascript" src="js/jquery-1.4.2.min.js" mce_src="js/jquery-1.4.2.min.js"></mce:script></head> <mce:script type="text/javascript"><!-- function fillBook(){ $.getJSON("getBook.action",{ran:Math.random()},function(json){ if(json.strAjaxChannel.length>0) { var obj=eval("("+json.strAjaxChannel+")"); $.each(obj,function(i,n){ mybook.options.add(new Option(n,i)); }); } }); } // --></mce:script> <body> <input type="button" value="ajax" onclick="fillBook()"> 书籍 : <select id="mybook"></select> </body> </html>
6.测试 。
url: http://localhost:8080/TestStruts2/