spring mvc: 资源绑定视图解析器(不推荐)

spring mvc: 资源绑定视图解析器(不推荐)

不适合单控制器多方法访问,有知道的兄弟能否告知。

访问地址:

http://localhost:8080/guga2/hello/index

项目:guga2

包名:springresource

本例配置文件web.xml, applicationContext.xml, springresource-servlet.xml, views.properties

web.xml

 <!--配置文件路径-->
<context-param>
 	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!-- 字符过滤器 -->  
<filter>
   <filter-name>encodingFilter</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>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 


<!-- 监听转发 -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  <servlet>  
    <servlet-name>springresource</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>springresource</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  

  

applicactionContext.xml自动导入bean

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">


<!-- 默认:注解映射支持 -->		
<mvc:annotation-driven/>
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/>

<!-- 自动扫描包名,controller -->
<context:component-scan base-package="springresource"/>

</beans>

  

springresource-servlet.xml引入properties文件

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.ResourceBundleViewResolver">
      <property name="basename" value="views" />
</bean>			
	
					
</beans>	

  

views.properties

hello.(class)=org.springframework.web.servlet.view.JstlView
hello.url=/WEB-INF/jsp/hello.jsp

  

HelloController.java

package springresource;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
public class HelloController {

	
	@RequestMapping(value="/hello/index", method=RequestMethod.GET)
	public String index(ModelMap model)
	{
		model.addAttribute("message", "hello index , Spring MVC资源绑定视图解析器!");
		return "hello";
	}
	
	
	@RequestMapping(value="/hello/bate", method=RequestMethod.GET)
	public String bate(ModelMap model)
	{
		model.addAttribute("message", "hello bate, Spring MVC资源绑定视图解析器!");
		return "hello_bate";
	}
	
}

  

jsp

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>hello index</title>
</head>
<body>

hi,${message}

</body>
</html>

  

猜你喜欢

转载自www.cnblogs.com/achengmu/p/9024492.html