Application of Jquery,ajax and JSON technology under SPRINGMVC

Source: Internet
Author: User

    1. Build the spring MVC environment and import the packages required for SPRINGMVC development

    2. Web. XML configuration

<?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/ Java ee " xmlns:web=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "          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 ">   <welcome-file-list>    <welcome-file>/index.jsp</welcome-file >  </welcome-file-list>  <context-param>    < param-name>contextconfiglocation</param-name>    <param-value>classpath:/ applicationcontext.xml</param-value>  </context-param>  <!--  Configure Spring Boot listener ingress &NBSP;--&GT;&NBSP;&NBsp;<listener>    <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class>  </listener>   <filter>    <filter-name>encodingFilter</filter-name>     <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class >    <init-param>      <param-name>encoding </param-name>      <param-value>UTF-8</param-value>     </init-param>    <init-param>       <param-name>forceencoding</param-name>      <param-value >true</param-value>    </init-param>  </filter>   <!-- encoding filter for jsp page -->   <filter-mapping>    <filter-name>encodingfilter</filter-name >    <url-pattern>/*</url-pattern>  </filter-mapping>   <!--  Configuration springmvc start Dispatcherservlet portal  -->  <!--  Central Controller  -->   <servlet>    <servlet-name>springMVC</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <init-param>      <param-name> contextconfiglocation</param-name>      <param-value>classpath:/ spring-mvc.xml</param-value>    </init-param>    < Load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>     <servlet-name>springmvc</servlet-name>    <!-- struts wont use/*, SPRINGMVC  -->     <url-pattern>/</url-pattern>  </servlet-mapping>  < servlet-mapping>  <!--here without *.js words, can not be src= "" Reference WebApp under the file, other similar-->    < Servlet-name>default</servlet-name>    <url-pattern>*.js</url-pattern >  </servlet-mapping>  <servlet-mapping>    < Servlet-name>default</servlet-name>    <url-pattern>*.css</url-pattern >  </servlet-mapping>  <servlet-mapping>    < Servlet-name>default</servlet-name>    <url-pattern>*.jpg</url-pattern >  </servlet-mapping>  <display-name>Archetype Created Web  application</display-name>&Lt;/web-app>3.===================spring-mvc.xml content (I put it in the resources directory) ===================================< ? 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:p= "http://www.springframework.org/schema/p"        xmlns:mvc= "http// Www.springframework.org/schema/mvc " xmlns:context=" Http://www.springframework.org/schema/context "        xmlns:util= "Http://www.springframework.org/schema/util"  xsi: Schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans-4.2.xsd            http:// www.springframework.org/schema/context http://www.springframework.org/schema/context/ spring-context-4.2.xsd             http://www.springframework.org/schema/mvc http:// www.springframework.org/schema/mvc/spring-mvc-4.2.xsd             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-4.2.xsd ">    <!--  Activate annotation mode, such as @controller -->     <mvc:annotation-driven />    <!--  Scans the annotations of classes in the package, creating beans and automatic dependency injection   -->    <context:component-scan base-package= "Cn.ys.controller"  />    <bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter " />     <bean id= "Viewresolver"  class= " Org.springframework.web.servlet.view.InternalResourceViewResolver ">         <property name= "prefix" >            <value>/</value>         </property>        < Property name= "suffix" >            < value>.jsp</value>        </property>     </bean></beans>4.=============== write a controller in the output JSON format (Jkson and other jar packages) ======
package cn.ys.controller;import org.springframework.stereotype.controller;import  org.springframework.web.bind.annotation.requestmapping;import  org.springframework.web.bind.annotation.responsebody;import org.springframework.web.servlet.modelandview; import java.text.simpledateformat;import java.util.arraylist;import java.util.date;import  java.util.list;/***  @author   Dengcong   E-mail:[email protected]*  @version   creation Time: *   Class description      output a JSON array of length 10 */@Controllerpublic  class showcontroller {      @ResponseBody//Output JSON object annotations      @RequestMapping   (  "/ ShowTime " )     public list<string> showtime ()  {         SimpleDateFormat simpleDateFormat = new  SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");         strinG format = simpledateformat.format (New date ());         ArrayList<String> strings = new ArrayList<String> ();         for  (int i=0;i<10;i++) {             strings.add (format);        }         return strings;    }}5.================== index.jsp=============================
<%@ taglib prefix= "Form"  uri= "Http://www.springframework.org/tags/form"  %><%--   created by intellij idea.  user: administrator  date:  2017/10/6  Time: 19:48  To change this template use  File | settings | file templates.--%><%@ page contenttype= "text/html; Charset=utf-8 " language=" Java  %>


Application of Jquery,ajax and JSON technology under SPRINGMVC

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.