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

5 篇文章 0 订阅
3 篇文章 0 订阅

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

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

// springmvc-servlet 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">

<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<context:component-scan base-package="com.huawei.action"></context:component-scan>
<mvc:annotation-driven />

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

// 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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</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">
<script src="js/tools/jquery-1.7.1.js"></script>
<script src="js/tools/jquery.json-2.2.min.js"></script>
<script src="js/js/login.js"></script>
  </head>
  
  <body>
  <form id="form">
  <table>
  <tr>
  <td>用户名:<input id="name" name="name" type="text" style="width: 145px;" /></td>
  </tr>
  <tr>
  <td>密&nbsp;&nbsp;&nbsp;&nbsp;码:<input id="pwd" name="pwd" type="password" style="width: 145px;" /></td>
  </tr>
  <tr>
  <td>地&nbsp;&nbsp;&nbsp;&nbsp;址:<input id="address" name="address" type="text" style="width: 145px;" /></td>
  </tr>
  <tr height="15px;">
  </tr>
  <tr align="center">
  <td>
  <input id="logId" type="button" value="登 录" />
  <input id="sendJson" type="button" value="sendJson" />
  </td>
  </tr>
  </table>
  </form>
  </body>
</html>


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

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





















  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值