SpringMVC 视图解析器实例 (IntelliJ IDEA)

1.  视图解析器定义

把一个逻辑上的视图名称解析为一个真正的视图名称解析成一个真正的视图,即将逻辑视图的名称解析为具体的 View 对象, 让View对象去处理视图,并将带有返回数据的视图反馈给客户端。

视图解析器可以大大减少 Controller 中视图定义的代码。

2.  使用视图解析器前后对比 Controller 编写对比

下面两个 Controller 的效果是一样的。

 未使用视图解析器的 Controller:

@Controller
public class HelloWorldController{
 
    @RequestMapping("helloWorld")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 创建模型
        ModelAndView modelAndView = new ModelAndView();
 
        // 向 模型 中添加数据
        modelAndView.addObject("message", "Hello World");
 
        // 返回逻辑视图
        modelAndView.setViewName("/WEB-INF/jsp/helloWorld.jsp");
        return modelAndView;
    }
}

使用视图解析器改写该 Controller:

// @Controller 标志是一个控制器
@Controller
public class HelloWorldController{

    @RequestMapping("helloWorld")
    public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model)
            throws Exception {
        // 向 模型 中添加数据
        model.addAttribute("message", "Hello World");

        // 自动添加前缀和后缀 ,相当于 /WEB-INF/jsp/helloWorld.jsp
        // 前缀后缀在视图解析器中配置
        return "helloWorld";
    }
}

前端控制器 xml 文件中需要配置视图解析器:

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

3. 工程实例目录

首先,你需要会不用视图解析器创建一个 SpringMVC 工程,SpringMVC 工程实例,点击前往然后与之比较。视图解析器有很多种,这里使用的是日常开发中比较常用的  InternalResourceViewResolver。 

工程目录如图所示。

4. 源文件的编写

4.1 xml 文件配置

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">
  <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>

  <!-- SpringMVC 的前端控制器 -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- 拦截了以 ".form" 结尾的URL请求,Web 应用接收到该类请求之后 , 会调用前端控制器-->
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

dispatcher-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"
       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">

  <!-- 配置视图解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
  <!-- 扫描配置 -->
  <context:component-scan base-package="com.study.springmvc.contoller"></context:component-scan>

</beans>

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- empty -->
</beans>

4.2 jsp 文件编写

helloUser.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Yimso
  Date: 2019/3/2
  Time: 10:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>

helloWorld.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Yimso
  Date: 2019/3/1
  Time: 16:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>

4.3 java 源文件编写

HelloUserController.java:

package com.study.springmvc.contoller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// @Controller 标识是一个控制器
@Controller
public class HelloUserController {

    // url请求中必须包含一个 username 的参数
    @RequestMapping(value = "/helloUser", params = "username")
    public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model)
            throws Exception {

        // 向 模型 中添加数据
        model.addAttribute("message", "Hello," + request.getParameter("username") + "!");

        return "helloUser";
    }
}

HelloWorldController.java : 

package com.study.springmvc.contoller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// @Controller 标志是一个控制器
@Controller
public class HelloWorldController{

    @RequestMapping("helloWorld")
    public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model)
            throws Exception {
        // 向 模型 中添加数据
        model.addAttribute("message", "Hello World");

        return "helloWorld";
    }
}

test.java:

package com.study.springmvc;

public class test {
    // empty
}

5 项目测试

5.1 测试helloWorld

访问 http://localhost:8080/helloWorld.form,运行结果如图所示:

 

5.2 测试helloUesr

访问http://localhost:8080/helloUser.form?username=springmvc,运行结果如图所示:

END。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值