基於springMVC簡單的請求和響應

1. 添加maven依賴:html

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

2. 工程總必定(建立)要有:dispatcher-servlet.xml。並在web.xml中引入這個配置文件:前端

web.xmljava

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--1表示要啓動的時候加載DispatcherServlet,這裏默認加載固定命名的dispatcher-servlet.xml
        固然,能夠重命名,而後告訴制定的路徑就能夠了!-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--配置請求過濾器,只接收*.do的請求-->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- 配置默認啓動的主頁 START  -->
    <welcome-file-list>
        <welcome-file>/html/TEST.html</welcome-file>
    </welcome-file-list>
</web-app>

dispatcher-servlet.xml:這裏配置一個bean是採用基於XML的自動掃描方式來裝載!jquery

<?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"
       xsi:schemaLocation="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.xsd">

    <!-- 自動掃描方式裝載bean -->
    <context:component-scan base-package="com.any.demoSpring.controller"/>
</beans>

3. 前端TEST.html:剛咱們再web.xml中配置了默認啓動主頁是:/html/TEST.html,因此咱們在這個頁面使用jQuery發起ajax http請求:web

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>TEST</title>
    <style>
        body{
            text-align: center;
        }
    </style>
</head>
<body>
<div>
    <input id="inVal" type="text" /><button id="btn">發送</button>
</div>
<script src="../js/lib/jquery-3.2.1.js"></script>
<script>
    $('#btn').click(function () {
        var inVal =  $('#inVal').val();
        $.ajax({
            url: "postTest.do",
            type: "POST",
            dataType: "text",
            contentType:"application/text;charset=UTF-8",
            data:inVal,
            success: function(data) {
               alert(data);
            },
            error: function() {
                alert("請求出錯!");
            }
        });

    });

</script>
</body>
</html>

4. 後臺:剛咱們在dispatcher-servlet.xml配置了一個掃描的controller包。因此咱們只需在這個com.any.demoSpring.controller包下建立一個類,並編寫響應方法便可:ajax

package com.any.demoSpring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @RequestMapping(value="postTest.do",method= RequestMethod.POST,produces = "application/text;charset=UTF-8;")
    @ResponseBody
    public String postTest(@RequestBody String str){
        System.out.print("\n後臺接收到前臺的數據="+str+"\n");
        return "來自後臺的數據:"+str;
    }
}

5.啓動Tomcat操做!spring

相關文章
相關標籤/搜索