SpringMVC(二)配置视图解析器
一、为什么使用视图解析器
如果有相同子路径的多个.jsp可能要进行跳转,就可以用视图解析器将共同部分的子路径提出来,简化书写。
EG:
@Controller
public class Test {
@RequestMapping("hello")
public ModelAndView hello(){
ModelAndView model = new ModelAndView();
model.setViewName("/WEB-INF/jsp/test.jsp");
// model.setViewName("test");
return model;
}
@RequestMapping("kk")
public ModelAndView kk(){
ModelAndView model = new ModelAndView();
model.setViewName("/WEB-INF/jsp/test.jsp");
// model.setViewName("test");
return model;
}
}
视图解析器:
<!--视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
prefix是前缀,suffix是后缀
二、视图解析器举例
1、Test类
@Controller public class Test { @RequestMapping("hello") public ModelAndView hello(){ ModelAndView model = new ModelAndView(); // model.setViewName("/WEB-INF/jsp/test.jsp"); model.setViewName("test"); return model; } //.jsp文件是 HTML页面加上Java代码的组合 //直接 /index.jsp 是可以访问到的,但是 /kk.jsp 就不行(写全路径也是不可以的), //这是因为 WEB-INF是安全的目录,里面的文件不能被直接访问到,只能通过视图解析器跳转过去进行访问 //简化写法(直接return jsp文件名): @RequestMapping("kk") public Object kk(){ //跳转到kk页面 return "kk"; } }
2、web/WEB-INF/jsp 中的 kk.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body><h1><font color="red"> 我是kk</font></h1>
</body>
</html>
3、src/ 下的 spring.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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.icss"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
</beans>
三、引入css
1、web/css/ 下的 style.css文件:
#kk{ color:red }
2、web/WEB-INF/jsp/ 下的 kk.jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <link href="css/style.css" type="text/css" rel="stylesheet"> </head> <body> <h1><font color="red"> 我是kk</font></h1> <div id="kk"> 阿斯蒂芬撒打发斯蒂芬 </div> </body> </html>
可以发现 引入了kk这个id选择器,但还是不能使 阿斯蒂芬撒打发斯蒂芬 这段字符串变为红色
这就需要在 src/spring.xml文件里打开css资源。
因为任何访问都算请求,请求的话就会走到 / ,也就是web目录下面。
虽然这里的css在它下面,但还是需要在spring.xml文件找中打开请求。
也就是资源文件处理:
需要导mvc:
<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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
然后使用:
<!-- 代表了处理器映射DefaultAnnotationHandlerMapping和处理器适配器AnnotationMethodHandlerAdapter, 同时可以使用json--> <mvc:annotation-driven></mvc:annotation-driven> <mvc:resources location="/css/" mapping="/css/**"></mvc:resources> <mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
注意:mvc相关的东西使用的时候一般都要开一下最上面的注解映射器<mvc:annotation-driven>