SpringMvc + jQuery + json(JSON接受和发送)

// web.xml 配置


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">

 
    index.jsp
 

 
  springmvc
  org.springframework.web.servlet.DispatcherServlet
 

 
  springmvc
  *.action
 

 
contextConfigLocation
/WEB-INF/springmvc-servlet.xml


org.springframework.web.context.ContextLoaderListener

// springmvc-servlet 配置


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">










// Entity: 实体类

package com.huawei.entity;
public class User {
private String id;
private String name;
private String pwd;
private String address;
public User() {
super();
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

// action 用于接受前台的数据和返回数据给前台

package com.huawei.action;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huawei.entity.User;

@Controller
public class UserAction {

@RequestMapping(value="/user", method = RequestMethod.GET)
@ResponseBody
public User login(HttpServletRequest request)
{

String parma = request.getParameter("name");
String name = null;

try 
{
name = new String (parma.getBytes("ISO-8859-1"),"gbk");

catch (UnsupportedEncodingException e) 
{
e.printStackTrace();
}

String pwd = request.getParameter("pwd");
User user = new User();
user.setName(name);
user.setPwd(pwd);

return user;
}

@RequestMapping(value="/json", method = RequestMethod.GET)
@ResponseBody
public User sendJson(HttpServletRequest request){

String parmaName = request.getParameter("name");
String parmaAddress = request.getParameter("address");

String name = null;
String address = null;

try 
{
name = new String (parmaName.getBytes("ISO-8859-1"),"utf-8");
address = new String (parmaAddress.getBytes("ISO-8859-1"),"utf-8");

catch (UnsupportedEncodingException e) 
{
e.printStackTrace();
}

String pwd = request.getParameter("pwd");
User user = new User();
user.setName(name);
user.setPwd(pwd);
user.setAddress(address);

return user;
}
}

// jQuery 文件

$(function(){
$("#logId").click(function(){
var name = $("#name").val();
var pwd = $("#pwd").val();
var prarm = "name=" + name + "&pwd=" + pwd;

$.ajax({
type : "get",
url : "user.action?method=login",
data : prarm,
dataType : "json",
success : function(msg){
alert("user.name : " + msg.name + "\r" + "user.pwd : " + msg.pwd);
},
error : function(){
alert('ajax-login-error');
}
});
});

$("#sendJson").click(function(){
var userJson = $("form").serialize();
//alert(userJson);
$.ajax({
type : "get",
url : "json.action?method=sendJson",
data : userJson,
dataType : "json",
success : function(msg){
alert("user.name : " + msg.name + "\r" + 
 "user.pwd : " + msg.pwd + "\r" + 
 "user.address : " + msg.address);
},
error : function(msg){
alert("ajax-sendJson-error");
}
})
})
})

// jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



 
   
    My JSP 'index.jsp' starting page


   





 
  
 
 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
用户名:
密    码:
地    址:

 
 
 

 

 


** 主要功能1:将表单数据装成JSON发送给后台,十分方便快捷;

    主要功能2: 后台返回JSON数据给前台。





















你可能感兴趣的:(SpringMvc,jQuery,json,jsp,web,java,springMvc,json,web,jqeury)