springmvc-210802-04—视图解析器
用户无法直接访问WEB-INF下的资源
springmvc.xml(声明springmvc框架中的视图解析器)
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bgy.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
MyController.java(使用视图解析器)
package com.bgy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping(value = "/some.do")
public ModelAndView doSome(){
ModelAndView mv= new ModelAndView();
mv.addObject("msg","这是我第一次使用springmvc框架");
mv.addObject("function","执行了doSome方法");
mv.setViewName("show");
return mv;
}
@RequestMapping(value = {"/other.do","/second.do"})
public ModelAndView doOther(){
ModelAndView mv= new ModelAndView();
mv.addObject("msg","这是我第一次使用springmvc框架");
mv.addObject("function","执行了doOther方法");
mv.setViewName("other");
return mv;
}
}
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"
metadata-complete="true">
<servlet>
<servlet-name>springmvc</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
index.jsp(主界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springmvc</title>
</head>
<body>
<p>第一个springmvc项目</p>
<p><a href="some.do">发起some.do请求</a></p>
<p><a href="other.do">发起other.do请求</a></p>
</body>
</html>
show.jsp(显示界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>show</title>
</head>
<body>
<h3>show.jsp,,,request作用域获取结果</h3>
<br/>
<h3>msg数据:${msg}</h3>
<br/>
<h3>function数据:${function}</h3>
</body>
</html>
other.jsp(显示界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>other</title>
</head>
<body>
<h3>show.jsp,,,request作用域获取结果</h3>
<br/>
<h3>msg数据:${msg}</h3>
<br/>
<h3>function数据:${function}</h3>
</body>
</html>
运行结果