基于springMVC简单的请求和响应

1. 添加maven依赖:

<!-- 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.xml

<!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的自动扫描方式来装载!

<?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请求:

<!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包下创建一个类,并编写响应方法即可:

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操作!

 

转载于:https://my.oschina.net/u/3697586/blog/1827640

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值