SpringMVC框架下實現JSON(類方法中回傳數據到jsp頁面,使用jQuery方法回傳)


JSON的實現,即將需要的數據回傳到jsp頁面;
 1>.加入實現Json的三個架包到lib中;
2>.目標方法上邊加入注解,需要返回的值
3>.在jsp頁面中書寫jQuery方法;

eclipse中javaEE環境下的web.xml文件配置為:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  
     <!-- 配置SpringMVC的DispatcherServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
    <!-- 配置 HiddenHttpMethodFilter: 把 POST 請求轉為 DELETE、PUT 請求 -->
      <filter>
          <filter-name>HiddenHttpMethodFilter</filter-name>
          <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
      </filter>
      
      <filter-mapping>
          <filter-name>HiddenHttpMethodFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
  
</web-app>

 

spring的bean的配置文件為:springmvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- 配置自動掃描的包 -->
    <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
    
    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!--  
        default-servlet-handler 將在 SpringMVC 上下文中定義一個 DefaultServletHttpRequestHandler,
        它會對進入 DispatcherServlet 的請求進行篩查, 如果發現是沒有經過映射的請求, 就將該請求交由 WEB 應用服務器默認的 
        Servlet 處理. 如果不是靜態資源的請求,才由 DispatcherServlet 繼續處理

        一般 WEB 應用服務器默認的 Servlet 的名稱都是 default.
        若所使用的 WEB 服務器的默認 Servlet 名稱不是 default,則需要通過 default-servlet-name 屬性顯式指定
        
    -->
    <mvc:default-servlet-handler/>
    
    <!-- 一般都會配置這個 <mvc:annotation-driven ></mvc:annotation-driven>,
    由於。。。requestmapping請求實現不了,使用這個,會使requestmapping請求一定實現
    -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
</beans>

 

EmployeeDao類:

package com.atguigu.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.atguigu.springmvc.crud.entities.Department;
import com.atguigu.springmvc.crud.entities.Employee;

@Repository//標識持久層組件
public class EmployeeDao {
    
    private static Map<Integer, Employee> employees=null;
    
    @Autowired
    private DepartmentDao departmentDao;
    static{
        employees=new HashMap<Integer, Employee>();
        
        employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
        employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));
        employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));
        employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
        employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
    }
    
    private static Integer initId=1006;
    
    //添加數據的方法
    public void save(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        
        employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }
    
    //獲取全部的數據的方法
    public Collection<Employee> getAll(){
        return employees.values();
    }
    
    //獲取集合中的一個數據
    public Employee getEmployee(Integer id){
        return employees.get(id);
    }
    
    //刪除集合中的一個數據
    public void delect(Integer id){
        employees.remove(id);
    }
}

 

實現的handler 類方法:SpringMVCTest:

@Controller
public class SpringMVCTest {
    
    @Autowired
    private EmployeeDao employeeDao;/*
     * JSON的實現,即將需要的數據回傳到jsp頁面;
     * 1>.加入實現Json的三個架包到lib中;
     * 2>.目標方法上邊加入注解,需要返回的值
     * 3>.在jsp頁面中書寫jQuery方法;
     * */
    @ResponseBody
    @RequestMapping("/testJson")
    public Collection<Employee> testJson(){
        return employeeDao.getAll();
    }
    
    
}

 

jsp頁面:index.jsp;需要導入jQuery架包,並且用jQuery方法,實現數據回顯到jsp頁面;點擊

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(function(){
        $("#testJson").click(function(){
            var url=this.href;
            var args={};
            
            $.post(url,args,function(data){
                for(var i=0;i<data.length;i++){
                    var id=data[i].id;
                    var lastName=data[i].lastName;
                    alert(id+":"+lastName);
                }
            });
        });
    })
    
    
</script>
</head>
<body>
    
    
    <a href="testJson" id="testJson">Test Json</a>
    <br><br>
    
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM