标签:mvc 前端 图片 control ota coding systems app ret
视图解析器是用来 接收并处理 经过处理器适配器调用具体的controller后 生成的逻辑视图的, 它接受 DispatcherServlet传过来的ModelAndView, 然后将ModelAndView数据填充到相应的视图中, 最后返回 一个将数据填充后的视图 给 DispatcherServlet(前端控制器/中心控制器)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="要访问的路径的前缀名"/>
<!--后缀-->
<property name="suffix" value="要访问的路径的后缀名"/>
</bean>
那么,现在本人来展示下添加视图解析器后的书写格式:
首先,本人给出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>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
接下来,本人给出一个配置文件:
<?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/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="edu.youzg.about_spring_mvc"/>
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
现在,本人再来给出用注解方式配置的控制器:
package edu.youzg.about_spring_mvc.controllor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @Author: Youzg
* @CreateTime: 2020-05-09 23:00
* @Description: 带你深究Java的本质!
*/
@Controller
public class MyController {
@RequestMapping("/how")
public String myFun(Model model) {
// 封装对象,放在Model中
model.addAttribute("msg", "I‘m pretty good!");
// 要进行内部转发的页面
return "howAreYou";
}
@RequestMapping("/hello")
public String world(Model model) {
model.addAttribute("msg", "how are you?");
// 要进行内部转发的页面
return "index";
}
}
那么,现在本人来访问 http://localhost:8080/hello 浏览器会出现如下显示结果: 而当我们访问 http://localhost:8080/how 时, 则会显示如下页面:
标签:mvc 前端 图片 control ota coding systems app ret
原文地址:https://www.cnblogs.com/codderYouzg/p/12928307.html