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>

  

  

spring mvc:内部资源视图解析器2(注解实现)@Controller/@RequestMapping的更多相关文章

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

    spring mvc:内部资源视图解析器(注解实现)@Controller/@RequestMapping 项目访问地址: http://localhost:8080/guga2/hello/prin ...

  2. Spring MVC内部资源视图解析器

    InternalResourceViewResolver用于将提供的URI解析为实际URI.下面的示例演示如何在Spring Web MVC框架中使用SpringResultViewResolver. ...

  3. Spring MVC的多视图解析器配置及与Freemarker的集成

    一.从freemarker谈起 Freemarker使用模板技术进行视图的渲染.自从看了Struts标签.Freemarker.JSTL的性能对比后,我毅然决定放弃Struts标签了!效率太差…… S ...

  4. Spring MVC-视图解析器(View Resolverr)-内部资源视图解析器(Internal Resource View Resolver)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_internalresourceviewresolver.htm 说明:示例基于S ...

  5. SpringMvc之handler深入AbstractControllerhe和MultiActionController和内部资源视图解析器

    AbstractControllerhe 若处理器继承自AbstractController类,那么该控制器就具有了一些新功能.因为AbstractController类还继承自一个父类WebCont ...

  6. spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

    spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...

  7. spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver

    spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...

  8. Spring Web MVC 多viewResolver视图解析器解决方案

    viewResolver的定义如下: public interface ViewResolver { View resolveViewName(String viewName, Locale loca ...

  9. Spring MVC参数方法名称解析器

    以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的参数方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...

随机推荐

  1. php微信支付回调验证

    //字典排序拼接字符串 function getWxPaySignature($arr){ ksort($arr); $str = ''; foreach ($arr as $k=>$a){ $ ...

  2. HDU3123:GCC(同余模简单题)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3123 题意很简单,就是同余模的简单应用. 代码如下: #include <iostream> ...

  3. Django中contenttype的应用

    content_type表将app名称与其中的表的关系进行保存 通过下边的示例来理解content_type的具体应用: models: from django.db import models fr ...

  4. 如何在VC6.0下用pthread.h这个头文件

    如何在VC6.0下用pthread.h这个头文件   1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可) http://so ...

  5. SQL各种连接——自连接、内连接、外连接、交叉连接的使用

    首先准备了两个表 (Student 和 Course),其中 Student 表中的 C_S_Id 字段为外键列,关联的是 Course 表的 C_Id 主键列. 内连接(inner join):满足 ...

  6. hdu2597 Simpsons’ Hidden Talents

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2594 题目: Simpsons’ Hidden Talents Time Limit: 2000/1000 ...

  7. NGUI基本事件

    You can add the following functions to your scripts placed on widgets or in-game objects with a coll ...

  8. eclipse 编译的时候 自动把SDK需要放入libs里面的so文件给删除了

    解决方法: 右击Project,选Properties->Builders, 把CDT Builder 关掉. 这样就不会编译了.包括c++的代码也不会编译.. 治标不治本啊...以后c++代码 ...

  9. java: -source 1.6 中不支持 switch 中存在字符串

    最近在使用IDEA进行单个文件编译的时候给我报错,如题. 解决办法:将 Modules --->Sources ---> Language level 改为 7.0就ok了.

  10. Statement与PreparedStatement

    Statement 用于通用查询,能批处理 PreparedStatement(简称PS) 用于执行参数化查询,能批处理 什么是参数化查询? 指在设计与数据库链接并访问数据时,在需要填入数值或数据的地 ...