Spring MVC:控制器类名称处理映射
控制器类名称处理映射的好好处是:
如果项目是hello,WelcomeController是控制器,那么访问地址是:
http://localhost:8080/hello/welcome
http://localhost:8080/hello/welcome.html
http://localhost:8080/hello/welcomeaaa.html
http://localhost:8080/hello/welcome([a-zA-Z*]).html 这样的无限配置
需要引入相应的类
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
然后接着添加想要访问的控制器类bean如:
<bean class="chapter2.web.controller.HelloController" />
<bean class="chapter2.web.controller.WelcomeController" />
来看实例:
WelcomeController.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; public class WelcomeController extends AbstractController { @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
ModelAndView mv = new ModelAndView("welcome");
mv.addObject("message", "welcome spring mvc!");
return mv;
} }
在xxx-servlet.xml中添加支持的spring类
<!-- 控制器类名称处理映射 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="chapter2.web.controller.WelcomeController"/>
welcome.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>welcome</title>
</head>
<body> <h2>welcome</h2>
${message} </body>
</html>
运行项目:
http://localhost:8080/hello/welcome
http://localhost:8080/hello/welcome.html
http://localhost:8080/hello/welcomeaaa.html
Spring MVC:控制器类名称处理映射的更多相关文章
- Spring MVC控制器类名称处理映射
以下示例显示如何使用Spring Web MVC框架使用控制器类名称处理程序映射. ControllerClassNameHandlerMapping类是基于约定的处理程序映射类,它将URL请求映射到 ...
- Spring MVC-处理程序映射(Handler Mapping)-控制器类名称处理程序映射(Controller Class Name Handler Mapping)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_controllerclassnamehandlermapping.htm 说明: ...
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver
spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...
- Spring入门(十四):Spring MVC控制器的2种测试方法
作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- Spring MVC控制器
Spring MVC中控制器用于解析用户请求并且转换为模型以提供访问应用程序的行为,通常用注解方式实现. org.springframework.stereotype.Controller注解用于声明 ...
- 1.2(Spring MVC学习笔记) Spring MVC核心类及注解
一.DispatcherServlet DispatcherServlet在程序中充当着前端控制器的作用,使用时只需在web.xml下配置即可. 配置格式如下: <?xml version=&q ...
- Spring MVC 配置类 WebMvcConfigurerAdapter
WebMvcConfigurerAdapter配置类是spring提供的一种配置方式,采用JavaBean的方式替代传统的基于xml的配置来对spring框架进行自定义的配置.因此,在spring b ...
随机推荐
- 读取xml文件,写入excel
在上一篇 Python写xml文件已经将所有订单写入xml文件,这一篇我们把xml文件中的内容读出来,写入excel文件. 输入xml格式: <?xml version="1.0&qu ...
- redis的客户端medis
medis是用js写的redis客户端工具,界面很好用,也很有用.点击这里可以下载到mac下的软件包. 下面是截图: 完结.
- 【HTML5游戏开发】简单的《找不同汉字版》,来考考你的眼力吧
一,准备工作 本次游戏开发需要用到lufylegend.js开源游戏引擎,版本我用的是1.5.2(现在最新的版本是1.6.0). 引擎下载的位置:http://lufylegend.googlecod ...
- SuperObject使用
SuperObject SuperObject第三方控件包,试用了下并编写了个DEMO,发现还是蛮强大.废话 少说,看看下面的代码就知道其强大: 1.JSON原始数据 { "name&q ...
- The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567
地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...
- 三张图看遍Linux性能监控、测试、优化工具
Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg在最近的 LinuxCon NA 2014 大 ...
- Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据
Magic Grid Time Limit:336MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Tha ...
- Jmeter使用流程及简单分析监控(转载)
转载自:https://www.cnblogs.com/linglingyuese/archive/2013/03/04/linglingyuese-one.html#undefined 一.安装Jm ...
- jxl.jar包,应该把它放在哪个文件下
①把鼠标放到自己的工程上右键 ②点击Properties or Build Path >> Configure Build Path, 然后弹出一个窗口,click AddExternal ...
- PreparedStatement和Statement区别详解
技术原理 该 PreparedStatement接口继承Statement,并与之在两方面有所不同: PreparedStatement 实例包含已编译的 SQL 语句.这就是使语句“准备好”.包含于 ...