Spring MVC

MVC分三层

1.业务模型

2.控制器

3.视图层

 

1、导入包:

 

<dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.10</version>
   </dependency>

   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.13.2</version>
   </dependency>
  <dependency>     <groupId>javax.servlet</groupId>     <artifactId>servlet-api</artifactId>     <version>2.5</version>   </dependency>   <dependency>     <groupId>javax.servlet.jsp</groupId>     <artifactId>jsp-api</artifactId>     <version>2.2</version>   </dependency>   
  <dependency>     <groupId>javax.servlet</groupId>     <artifactId>jstl</artifactId>     <version>1.2</version>   </dependency> </dependencies> <!--配置文件导不出去加上,资源过滤--> <build>   <resources>     <resource>       <directory>src/main/resources</directory>       <includes>       <include>**/*.properties</include>       <include>**/*.xml</include>       </includes>       <filtering>true</filtering>    </resource>    <resource>       <directory>src/main/java</directory>       <includes>         <include>**/*.properties</include>         <include>**/*.xml</include>       </includes>       <filtering>true</filtering>     </resource>   </resources> </build>

2、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--配置dispatchServlet:这个是SpringMVC的核心:请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--DispatcherServlet要绑定Spring的配置文件-->
        <!-- 这里要给配置文件设置参数-->
        <init-param>
            <!--我们要通过他绑定一个类-->
            <param-name>contextConfigLocation</param-name>
            <!--建议用classpath当前类的路径,classpath*会去所有的包里面去找-->
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--启动级别为1级,就是和服务器一起启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!--
    在springMCV中, / 和 /* 的区别
    /:只匹配所有的请求,不会去匹配jsp页面
    /*:匹配所有的请求,包括jsp页面
    一般我们只用到 /
    /*的话可能出现无限死循环,因为我们设置的后缀是jsp,所有会/.jspdawd.jsp
    -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

3、创建springmvc-servlet.xml文件

<?xml version="1.0" encoding="GBK"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.peng.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>


    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

4、创建web下面的视图层

  这里就不写了、、、、、、、

 

5、业务层

package com.peng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller//注解开发
//@RequestMapping("/hello")
public class HelloServlet {
    @RequestMapping("/h1")//注解定义浏览器地址
    public String user(Model model){
        //封装类
        model.addAttribute("daom","你好HelloController");
        return "hello";//被视图解析器处理,也就是先返回到xml中,然后返回到web项目的jsp文件中
    }
    @RequestMapping("/h2")
    public String user2(Model model){
        //封装类
        model.addAttribute("daom","你好Hello");
        return "hello";//被视图解析器处理
    }

}

 

 

RestFul风格就是改变传值的风格,更安全

@RequestMapping("/h2/{a}/{b}")
public String user2(@PathVariable int a,@PathVariable int b, Model model){
//封装类
int rs=a+b;
model.addAttribute("daom","你好Hello"+rs);
return "hello";//被视图解析器处理
}

 

 

排查错误

400的错误请求有问题
500的错误代码有问题

访问出现404,排查步骤

1、查看控制台输出,看一下是不是缺少l什么jar包
2、如果jar包存在,显示无法输出,就在IDEA的项目发布中,添加lib依赖
步骤:ctrl+alt+shift+s 然后选择artifacts ,在项目里面创建lib把包导入进去

3、重启Tomcat即可解决

 

posted @ 2021-11-19 16:05  追星月?问酒缘。  阅读(25)  评论(0)    收藏  举报