SpringMVC实现步骤

SpringMVC依赖

<!-- https://mvnrepository.com/artifact/com.mangofactory/swagger-springmvc -->
    <dependency>
      <groupId>com.mangofactory</groupId>
      <artifactId>swagger-springmvc</artifactId>
      <version>1.0.2</version>
  	</dependency>

一、web.xml 配置

  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

二、spring.xml配置 springDispatcherServlet-servlet.xml文件名不许以-servlet.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/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
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 
 <!-- 1 配置需要自动扫描的包  -->
 <context:component-scan base-package="c.kgc.springmvc"></context:component-scan>
 
 
 <!--视图解析器,通用  -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/views/" />
   <property name="suffix" value=".jsp" />
 </bean>
 
</beans>

三、创建类

//注解代表此类为控制层  和web.xml的配置关联
@Controller
public class HelloWorld {
    @RequestMapping(value = "/orderGet",method = RequestMethod.GET)
    public String ordetGet(){
        System.out.println("get...");
            return "ok";
    }
    @RequestMapping(value = "/ordetPut/{id}",method = RequestMethod.PUT)
    public String ordetPut(@PathVariable("id") Integer id){
        System.out.println("id"+id);
        return "ok";
    }

    @RequestMapping(value = "/orderDelete/{uuid}",method = RequestMethod.DELETE)
    public String ordetDelete(@PathVariable("uuid") Integer uuid){
        System.out.println(uuid);
        return "ok";
    }
    @RequestMapping(value = "/orderPost",method = RequestMethod.POST)
    public String ordetDelete(){
        System.out.println("post...");
        return "ok";
    }
    /**
     * 动态获取项目路径
     * @param request
     * @param response
     * @param writer
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/testAPI",method = RequestMethod.GET)
    public String testAPI(HttpServletRequest request, HttpServletResponse response, Writer writer) throws IOException {
        System.out.println("====request:"+request.getContextPath());
        writer.write("welcome at bdqn......");
        writer.flush();
        return "ok";
    }

    /**
     * 将前台文本框的数据自动封装到实体类中
     * @param user
     * @return
     */
    @RequestMapping(value = "/pojo",method = RequestMethod.POST)
    public String pojo(User user){
        System.out.println(user.toString());
        return"ok";
    }

    /**
     *获取
     * @param sid
     * @return
     */
    @RequestMapping(value="/cookieValue",method=RequestMethod.GET)
    public String cookieValue(@CookieValue("JSESSIONID") String sid)
    {
        System.out.println(sid);
        return "ok";
    }

    /**
     * 获取请求头的值
     * @return
     */
    @RequestMapping(value = "/reuqestHeader",method = RequestMethod.GET)
    public String reuqestHeader(@RequestHeader(value="Host") String host){
        System.out.println("reuqestHeader...");
        System.out.println(host);
        return "ok";
    }


    /**
     * 获取前台数据   他主要获取name  age等等...
     * @return
     */
    @RequestMapping(value = "/reuqestParams",method = RequestMethod.GET)
    public String reuqestParams(@RequestParam(value = "age",required = false,defaultValue = "18") Integer age){
        System.out.println("reuqestParams...");
        System.out.println(age);
        return "ok";
    }
    /**
     * 获取前台数据   他主要获取id的
     * @return
     */
    @RequestMapping(value = "/pathVirable/{uuid}",method = RequestMethod.GET)
    public String pathVirable(@PathVariable("uuid") Integer uuid){
        System.out.println("pathVirable...");
        System.out.println(uuid);
        return "ok";
    }


    /**
     * ?代表一表匹配符  *匹配所有
     * @return
     */
    @RequestMapping(value = "/Ant??",method = RequestMethod.GET)
    public String ant(){
        System.out.println("ant...");
        return "ok";
    }

    /**
     * 服务器端口号
     * @return
     */
    @RequestMapping(value = "/header",headers = {"Host=localhost:8080"},method = RequestMethod.GET)
    public String header(){
        System.out.println("header...");
        return "ok";
    }

    /**
     * age 和 tel jsp页面要给出值
     * @return
     */
    @RequestMapping(value = "/params",params = {"age","tel!=110"},method = RequestMethod.GET)
    public String params(){
        System.out.println("params...");
        return "ok";
    }


    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String hello(){
        System.out.println("hello...");
        return "ok";
    }
}

四、JSP

<body>
<fieldset>
    增加post:
    <form action="${pageContext.request.contextPath }/orderPost" method="post">
        <input type="submit" value="post提交">
    </form>
    删除delete
    <form action="${pageContext.request.contextPath }/orderDelete/123" method="post">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="delete提交">
    </form>
    修改PUT:
    <form action="${pageContext.request.contextPath }/ordetPut/222" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="PUT提交">
    </form>
    查询get
    <form action="${pageContext.request.contextPath }/orderGet" method="get">
        <input type="submit" value="GET提交">
    </form>
</fieldset>

<form action="${pageContext.request.contextPath }/pojo" method="post">
    用户名<input type="text" name="name" value="z3">
    密码<input type="password" name="password" value="abc123">
    年龄<input type="text" name="age" value="50">
    邮箱<input type="text" name="email" value="z3@qq.com">
    <input type="submit" value="tijiao">
</form>

<%--动态获取项目路径--%>
this testAPI <a href="${pageContext.request.contextPath }/testAPI">this testAPI </a><br>
<%--获取JSESSIONID--%>
this cookieValue <a href="${pageContext.request.contextPath }/cookieValue">this cookieValue </a><br>
<%--获取前台数据--%>
this reuqestParams <a href="${pageContext.request.contextPath }/reuqestParams">this reuqestParams </a><br>
<%--获取请求头的数据--%>
this reuqestHeader <a href="${pageContext.request.contextPath }/reuqestHeader">this reuqestHeader </a><br>
<%----%>
this hello <a href="${pageContext.request.contextPath }/hello">this hello </a><br>
<%--获取前台数据--%>
this params <a href="${pageContext.request.contextPath }/params?age=20&tel=120">this params </a><br>
<%--获取请求头的数据--%>
this header <a href="${pageContext.request.contextPath }/header">this header </a><br>
<%--?好代表一个占位符,*代表多个占位符--%>
this ant <a href="${pageContext.request.contextPath }/Antqq">this ant </a><br>
<%--获取前台数据--%>
this pathVirable <a href="${pageContext.request.contextPath }/pathVirable/123">pathVirable</a><br>
</body>

五、如果删除或者修改数据时需要在web.xml文件中配置拦截

<!--  使用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>

工程结构

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值