SPRINGMVC implementation of JSON in the framework (class method in the callback data to the JSP page, using the jquery method callback)

Source: Internet
Author: User

JSON implementation, the data will be needed to upload to the JSP page;
1> Add three shelves to Lib that implement JSON;
2>: Add annotations to the top of the target method and need to return the value
3> Write the jquery method in the JSP page;

The Web. xml file in the Java EE environment in Eclipse is configured as:

<?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" > <!--configuration 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-o n-startup> </servlet> <servlet-mapping> <servlet-name>springdispatcherservlet</servle T-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--configuration Hiddenhttpmethodfil ter: Convert POST request to DELETE, put request-<filter> <filter-name>hiddenhttpmethodfilter</filter-na Me> <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>

The spring bean's configuration file is: 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><!--Configure automatic scanning of packages--<context:component-scan base- Package= "Com.atguigu.springmvc" ></context:component-scan> <!--configuration View Resolver--<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/        web-inf/views/"></property> <property name=" suffix "value=". jsp "></property> </bean> <!--default-servlet-handler will define a Defaultservlethttprequesthandler in the SPRINGMVC context, which will screen for requests to enter Dispatcherservlet if it finds no mapped requests , the request is referred to the WEB application server's default Servlet processing. If the request is not a static resource, the Dispatcherservlet continues to process the generic WEB application server The default Servlet name isdefault. If the default Servlet name for the WEB server you are using is notdefault, you need to passdefault-servlet-The Name property explicitly specifies--&LT;MVC:default-servlet-handler/> <!--will typically configure this <mvc:annotation-driven ></mvc:annotation-driven>, as a result of ... Requestmapping request can not be implemented, using this, will make the requestmapping request must be implemented-<mvc:annotation-driven></mvc:annotation-driven> </beans>

EmployeeDAO class:

 PackageCom.atguigu.springmvc.crud.dao;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Repository;Importcom.atguigu.springmvc.crud.entities.Department;ImportCom.atguigu.springmvc.crud.entities.Employee; @Repository//Identify persistence layer components Public classEmployeeDAO {Private StaticMap<integer, employee> employees=NULL; @AutowiredPrivateDepartmentdao Departmentdao; Static{Employees=NewHashmap<integer, employee>(); Employees.put (1001,NewEmployee (1001, "E-aa", "[email protected]", 1,NewDepartment (101, "D-aa"))); Employees.put (1002,NewEmployee (1002, "E-BB", "[email protected]", 1,NewDepartment (102, "D-BB"))); Employees.put (1003,NewEmployee (1003, "e-cc", "[email protected]", 0,NewDepartment (103, "D-CC"))); Employees.put (1004,NewEmployee (1004, "E-dd", "[email protected]", 0,NewDepartment (104, "D-DD"))); Employees.put (1005,NewEmployee (1005, "E-ee", "[email protected]", 1,NewDepartment ("D-ee"))); }        Private StaticInteger initid=1006; //ways to add data     Public voidSave (Employee employee) {if(Employee.getid () = =NULL) {Employee.setid (Initid++);        } employee.setdepartment (Departmentdao.getdepartment (Employee.getdepartment (). GetId ()));    Employees.put (Employee.getid (), employee); }        //ways to get all the data     PublicCollection<employee>GetAll () {returnemployees.values (); }        //gets a data from the collection     PublicEmployee GetEmployee (Integer id) {returnemployees.get (ID); }        //to delete a data from a collection     Public voidDelect (Integer id) {employees.remove (ID); }}

Implementation of the Handler class method: Springmvctest:

@Controller  Public class springmvctest {        @Autowired    private  EmployeeDAO EmployeeDAO; /*      * JSON implementation, the data will be required to upload to the JSP page;     * 1> add three racks of JSON to Lib;     * 2> the target method adds an annotation to the top, which needs to return the value     * 3> Write the jquery method in the JSP page     ;*    /@ResponseBody @RequestMapping ("/testjson"  )    public collection<employee> Testjson () {         return  Employeedao.getall ();    }        }

JSP page: index.jsp; You need to import the jquery rack package, and use the JQuery method to implement the data echo to the JSP page; click

<%@ 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" >$ (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>

SPRINGMVC implementation of JSON in the framework (class method in the callback data to the JSP page, using the jquery method callback)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.