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. Docker中安装配置Oracle数据库

    本文使用的OS是Ubuntu([16.04.1_server][1])[注:Ubuntu是安装在vmware虚拟机上的]. 其他的Oracle连接工具:[sqldeveloper-4.1.5.21.7 ...

  2. js-jquery-002-条形码-一维码

    一.使用 官方地址:http://barcode-coder.com/en/barcode-jquery-plugin-201.html 1.js引用 <script type="te ...

  3. SAMA5D3 Xplained Board

    一.烧录 1)先在官网 http://www.at91.com/linux4sam/bin/view/Linux4SAM/Sama5d3XplainedMainPage 下载  linux4sam-p ...

  4. LNMP环境修改上传文件大小

    LNMP环境修改上传文件大小限制     如果网页上传文件大小大于nginx或php设定的值,就会造成无法上传,显示IO error错误. 如何解决这个问题呢?我们需要更改两个配置: 1.  ngin ...

  5. 《FTL之垃圾回收、写放大和OP 》总结

    来自 http://www.ssdfans.com/?p=1840: 写放大WA: 对空盘来说(未触发GC),写放大一般为1,即Host写入多少数据,SSD写入闪存也是多少数据量(这里忽略SSD内部数 ...

  6. dbms_advisor 手动生成段顾问建议!

    执行包需要dbms_advisor权限: sys@ORCL> grant advisor to u1; 授权成功. 创建段顾问任务,指定create_task的advisor_name参数为“段 ...

  7. Django restframwork实现自定义数据格式的分页与搜索

    最近因为在做分页时遇到的问题很多,页浪费了好多时间,所以记录一下.以后如遇到可用省去不必要的麻烦 restframwork中的官方文档对分页和搜索页进行了详细的介绍,但是我公司需要的return的js ...

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

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

  9. 自动填写IE的网页的输入框的内容

    procedure TForm1.PutData; var ShellWindow: IShellWindows; nCount: integer; spDisp: IDispatch; i,j,X: ...

  10. ES6 利用 Set 数组去重法

    例子: const set = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => set.add(x) ); const arr = [...set]; ...