RS
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
@Component
@Path("/users")
public class UserResource {
@GET
public Response login() {
String msg="[{text : '投票维护',leaf: true,url : null},{text : '投票结果',leaf: true,url : null}]";
return Response.status(200).entity(msg).build();
}
}
配置web.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class> com.sun.jersey.spi.spring.container.servlet.SpringServlet </servlet-class> <init-param> <param-name> com.sun.jersey.config.property.packages </param-name> <param-value>com.techiekernel.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
Extjs login.html
<HTML>
<HEAD>
<TITLE>Ext.Ajax. request提交json数据</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css"
href="../../ext-4.0/resources/css/ext-all.css" />
<script type="text/javascript" src="../../ext-4.0/bootstrap.js"></script>
<script type="text/javascript"
src="../../ext-4.0/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript">
function login() {
var requestConfig = {
url : '/vote/rest/users', //请求的服务器地址
jsonData : getJson(), //发送json对象
method : 'GET',
callback : function(options, success, response) { //回调函数
var msg = [ "请求是否成功:", success, "\n", "服务器返回值:",
response.responseText ];
alert(msg.join(''));
}
}
Ext.Ajax.request(requestConfig); //发送请求
/* Ext.Ajax.request({
url : '/vote/rest/users',
method : 'GET'
}) */
}
//生成json对象
function getJson() {
var name = document.forms['loginForm'].userName.value; //获取表单中的用户名信息
var pwd = document.forms['loginForm'].password.value; //获取表单中的密码信息
var jsonObj = { //创建包含用户名信息和密码信息的JSON对象
userName : name,
password : pwd
}
return jsonObj; //将JSON对象返回
}
</script>
</HEAD>
<BODY>
<br>
<form id='loginForm'>
用户名:<input name='userName' type='text'> 密 码:<input
name='password' type='password'> <input type='button'
value='登陆' οnclick='login()'>
</form>
</BODY>
</HTML>