SSM整合-part4-根据SpringMVC创建表现层

本文介绍了SpringMVC表现层的搭建过程。首先需修改web.xml文件,设定字符集增强并配置核心DispatchServlet,同时创建相应配置文件。接着修改Spring配置文件,使其不扫描SpringMVC的Bean。之后编写Rest风格的Controller,最后用PostMan测试,若有对应输出则搭建完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
我们知道SpringMVC做的主要其实是表现层的,所以这里我们就要动web.xml文件了。其实动的也很简单

  1. 加上设定字符集的增强
  2. 配置核心DispatchServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

  <servlet>
    <!--与普通的Servlet不同,这里我们使用的是DispatcherServlet-->
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--我们这里要告诉他如何初始化我们的DispatcherServlet,就是去读取spring-mvc.xml的文件,该文件确保了哪些类可以作为Bean被扫描到-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <!--“/”意味着就是任何路径都会触发这个Servlet-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>CharacterEncodingFilter</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>
  </filter>

  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

注意!这里配置核心DispatchServlet的时候,我们需要告诉他,他是根据什么配置来创建的,配置中包括比如哪些类(Bean)是需要被他管理的,所以有了这句<param-value>classpath*:spring-mvc.xml</param-value>,因此我们就需要再创建一个spring-mvc.xml的配置文件。配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--注意!这里我们只扫描controller包下的,所以我们这里可以去Spring的applicationContext文件里把他要扫描的包修改一下-->
    <context:component-scan base-package="com.itheima.controller"/>
    <!--开启springmvc注解驱动,对@ResponseBody的注解进行格式增强,追加其类型转换的功能,具体实现由MappingJackson2HttpMessageConverter进行-->
    <mvc:annotation-driven/>
</beans>

我们修改一下Spring配置文件applicationContext.xml,让他不要扫描SpringMVC的Bean,也就是Controller。

    <!--开启扫描组件,但是我们不扫描SpringMVCBean,所以设置Exclude File-->
    <context:component-scan base-package="com.itheima">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--同时启动MVC的注解驱动-->
    <mvc:annotation-driven/>

到这里SpringMVC就算配好了,我们来写一下Rest风格的Controller:

@RestController//这个相当于下面两个的结合体,这是Rest风格的
//@Controller
//@ResponseBody
@RequestMapping("/user")//相当于你这个Servlet的路径
public class UserController {

	//http://localhost/user?userName=SunLL&password=6745&realName=Five&gender=1&birthday=1996/11/05
    @PostMapping//这是提交(产生新的数据)
    public boolean save(User user){
        System.out.println("Save... "+user);
        return true;
    }

    @PutMapping
    public boolean update(User user){
        System.out.println("Update... "+user);
        return true;
    }

    @DeleteMapping("/{uuid}")
    public boolean delete(@PathVariable Integer uuid){
        System.out.println("Delete... "+uuid);
        return true;
    }


    //因为我们知道Rest的话,你是不在URL里面显示属性名的,所以我们直接拿值
    //而我们的参数名是需要与@GetMapping里面的对应的
    @GetMapping("/{uuid}")
    public User getUser(@PathVariable Integer uuid){
        System.out.println("Get "+uuid);
        return null;
    }
	
	//http://localhost/user/1/2
    @GetMapping("/{page}/{size}")
    public List getAll(@PathVariable Integer page,@PathVariable Integer size){
        System.out.println("Get page "+page+" size "+size);
        return null;
    }

	//http://localhost/user/login?userName=sunll&password=123
    @PostMapping("/login")//和另一个post方法做一个区分
    public User login(String userName, String password){
        System.out.println(userName+"   "+password);
        return null;
    }

}

基本上你看注释就明白了,首先我们先给整体配了个/user的路径,然后剩下的配参数,有的是基本数据类型有的则是引用类型,你参看注释就知道了。值得注意的就是save和login使用的都是@PostMapping,save好理解,login用post是因为为了确保账号密码安全。

如果使用PostMan发送请求都能有对应的输出的话,就代表测试通过,恭喜你SpringMVC表现层也搭建完成了!!!

资源下载链接为: https://pan.quark.cn/s/f989b9092fc5 HttpServletRequestWrapper 是 Java Servlet API 中的一个工具类,位于 javax.servlet.http 包中,用于对 HttpServletRequest 对象进行封装,从而在 Web 应用中实现对 HTTP 请求的拦截、修改或增强等功能。通过继承该类并覆盖相关方法,开发者可以轻松地自定义请求处理逻辑,例如修改请求参数、添加请求头、记录日志等。 参数过滤:在请求到达处理器之前,可以对请求参数进行检查或修改,例如去除 URL 编码、过滤敏感信息或进行安全检查。 请求头操作:可以修改或添加请求头,比如设置自定义的 Content-Type 或添加认证信息。 请求属性扩展:在原始请求的基础上添加自定义属性,供后续处理使用。 日志记录:在处理请求前记录请求信息,如 URL、参数、请求头等,便于调试和监控。 跨域支持:通过添加 CORS 相关的响应头,允许来自不同源的请求。 HttpServletRequestWrapper 通过继承 HttpServletRequest 接口并重写其方法来实现功能。开发者可以在重写的方法中添加自定义逻辑,例如在获取参数时进行过滤,或在读取请求体时进行解密。当调用这些方法时,实际上是调用了包装器中的方法,从而实现了对原始请求的修改或增强。 以下是一个简单的示例,展示如何创建一个用于过滤请求参数的包装器: 在 doFilter 方法中,可以使用 CustomRequestWrapper 包装原始请求: 这样,每当调用 getParameterValues 方法时,都会先经过自定义的过滤逻辑。 HttpServletRequestWrapper 是 Java Web 开发中一个强大的工具,它提供了灵活的扩展性,允许开发者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值