spring mvc:内部资源视图解析器2(注解实现)@Controller/@RequestMapping

spring mvc:内部资源视图解析器2(注解实现)  @Controller/@RequestMapping

访问地址:

http://localhost:8080/guga2/hello/good

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

项目:guga2

包名:springxmlviewresolver

 

此实例配置文件有: web.xml, applicationContext.xml, springxmlviewresolver-servlet.xml, views.xml

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>springxmlviewresolver</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>springxmlviewresolver</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>   

  

applicationContext.xml

自动扫描包名加载信息

<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="springxmlviewresolver"/>
			



</beans>

 

springxmlviewresolver-servlet.xml

自动设置内部资源解析视图

<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">
					
		
<!-- xmlviewResolver -->
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
	<property name="location">		
		<value>/WEB-INF/views.xml</value>
	</property>
</bean>		
					
</beans>

  

views.xml

内部解析视图实例

<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 id="hello" class="org.springframework.web.servlet.view.JstlView">		
	<property name="url" value="/WEB-INF/jsp/hello.jsp"/>	
</bean>
<bean id="hello_good" class="org.springframework.web.servlet.view.JstlView">		
	<property name="url" value="/WEB-INF/jsp/hello_good.jsp"/>	
</bean>
					
</beans>

  

注意此处:

<bean id="hello_good" class="org.springframework.web.servlet.view.JstlView">		
	<property name="url" value="/WEB-INF/jsp/hello_good.jsp"/>	
</bean> 

 

id名称,跟 jsp文件名称一致,同时要跟helloController对应方法中的hello/good返回的视图名称一致.

 

HelloController.java

package springxmlviewresolver;

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 printHello(ModelMap model)
	{
		model.addAttribute("message", "spring mvc framework 映射页面与请求实例!!!!!!");
		return "hello";
	}
	
	@RequestMapping(value="/hello/good", method=RequestMethod.GET)
	public String printGood(ModelMap model)
	{
		model.addAttribute("message", "spring mvc framework 映射页面与请求实例??????");
		return "hello_good";
	}
	
}

  

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 world</title>
</head>
<body>



${message}

</body>
</html>

  

 

 

 

  

 

posted @ 2018-05-10 14:55  穆晟铭  阅读(286)  评论(0编辑  收藏  举报