当前位置 : 主页 > 网页制作 > Dojo >

dijit.form.Select菜单项的数据通过rest加载

来源:互联网 收集:自由互联 发布时间:2021-06-15
添加rest服务所需jar包 略过 web.xml文件配置 ?xml version="1.0" encoding="UTF-8"?web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="

添加rest服务所需jar包

略过

web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Dojo-test</display-name>
	
	<context-param>
	    <param-name>resteasy.servlet.mapping.prefix</param-name>
	    <param-value>/rest</param-value>
	</context-param>
	
	<context-param>
		<param-name>resteasy.scan</param-name>
		<param-value>true</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>Resteasy</servlet-name>
		<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>Resteasy</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

编写测试类

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/echo")
public class Echo {
	@GET
	@Path("/getDistrict")
	//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	//@Produces("application/json")
	public District[] getDistrict(){
		District[] arrD = new District[7];
		District d = new District();
		
		d.id = "jn";
		d.name = "济南";
		arrD[0] = d;
		d = new District();
		d.id = "qd";
		d.name = "青岛";
		arrD[1] = d;
		d = new District();
		d.id = "zb";
		d.name = "淄博";
		arrD[2] = d;
		d = new District();
		d.id = "zz";
		d.name = "枣庄";
		arrD[3] = d;
		d = new District();
		d.id = "dy";
		d.name = "东营";
		arrD[4] = d;
		d = new District();
		d.id = "yt";
		d.name = "烟台";
		arrD[5] = d;
		d = new District();
		d.id = "jng";
		d.name = "济宁";
		arrD[6] = d;
		
		return arrD;
	}
	class District{
		public String id ;
		public String name;
	}
}

dojo页面

第一种加载方式,编程:

require([
	"dojo/store/JsonRest",
	"dijit/Tree",
	"dijit/tree/ObjectStoreModel",
	"dojo/domReady!",
	"dijit/form/Select",
     "dijit/form/FilteringSelect",
	"dojox/data/JsonRestStore",
	"dojo/data/ObjectStore"
], function(){
	var objectStore  = new dojo.store.JsonRest({
		target: "/dojo/rest/echo/getDistrict"
	});
	var dataStore = new dojo.data.ObjectStore({objectStore: objectStore});
	var dt = new dijit.form.Select({  
		 store: dataStore,
		 labelAttr:'name'
        },  
        "dtSelect");  
	dt.startup();
});
<div id = "dtSelect"></div>

第二种加载方式,声明:

<div data-dojo-type="dojo/store/JsonRest" data-dojo-id="restData" data-dojo-props='target: "/dojo/rest/echo/getDistrict"'></div>
<div data-dojo-type="dojo/data/ObjectStore" data-dojo-id="restStore" data-dojo-props="objectStore: restData"></div>
<div data-dojo-id="corpSelect" data-dojo-type="dijit/form/Select" data-dojo-props='store: restStore, labelAttr:"name"'></div>
网友评论