1.可以自动加载注解驱动,通过注解找到对应Controller

    <!-- spring MVC 注解驱动 --><mvc:annotation-driven></mvc:annotation-driven><!-- 配置自动扫描包 --><context:component-scan base-package="com.cc8w.Controller"></context:component-scan><!-- 配置视图解析器 --><bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/view/"></property>  <property name="suffix" value=".jsp"></property>  </bean> 

package com.cc8w.Controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
@RequestMapping("/HelloWorld")
public class HelloWorld {@RequestMapping("/hi.do")public String hi(String names,Model model){System.out.println("hi.dodo");model.addAttribute("hi", "hi123456");return "hi";}
}

显示结果正确.

2.配置HandlerMapping,根据beanName找到对应Controller

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><display-name>Spring-web02</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><!-- 配置HandlerMapping 根据beanname找到对应Controller --><bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean><!--配置beanname找到对应的Controller --><bean name = "/dogcontrollerdog" class = "com.cc8w.Controller.Dog"></bean><!-- 配置视图解析器 --><bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/view/"></property>  <property name="suffix" value=".jsp"></property>  </bean> </beans>

测试类(这里一定要继承org.springframework.web.servlet.mvc.AbstractController接口)

package com.cc8w.Controller;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 Dog extends AbstractController {public Dog(){System.out.println("13213"); }@Overrideprotected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {// TODO Auto-generated method stubSystem.out.println("456--访问到了---"); ModelAndView mav = new ModelAndView("dog");mav.addObject("hi", "467 th");return mav;}
}

模版目录和结果

3.配置HandlerMapping,根据URL找到对应Controller(主要是applicationContext.xml或<bean-name>-Servlet.xml的区别)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><!-- 配置HandlerMapping 根据简单URL找到对应Controller --><bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="/dogcontroller">dog</prop></props></property></bean><!--bean类 --><bean id = "dog"  class = "com.cc8w.Controller.Dog"></bean><!-- 配置视图解析器 --><bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/view/"></property>  <property name="suffix" value=".jsp"></property>  </bean> </beans>

上面的类,这个spring MVC配置文件,可以找到控制器,就不截 结果了.

4.配置HandlerMapping,通过控制器类名访问Controller,访问时类名首字母小写 小写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><!-- 配置HandlerMapping 通过控制器类名访问Controller,访问时类名首字母小写 --><bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean><!--bean类 --><bean  class = "com.cc8w.Controller.Dog"></bean><!-- 配置视图解析器 --><bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/WEB-INF/view/"></property>  <property name="suffix" value=".jsp"></property>  </bean> </beans>

http://localhost:8080/Spring-web02/dog 这样访问...

转载于:https://www.cnblogs.com/fps2tao/p/7274173.html

Spring MVC 的xml一些配置相关推荐

  1. Spring MVC Servlet XML文件配置

    1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://ww ...

  2. Spring MVC 无XML配置入门示例

    Spring MVC 无XML(纯 Java)配置入门示例 本示例是从<Spring in Action, Fourth Edition>一书而来,涉及的是书中5.1节部分内容,书中其实说 ...

  3. Spring MVC框架的高级配置

    前言: 本文将为您提供关于Spring MVC框架的配置技巧,以帮助管理基于Spring的web应用程序的多个实例.本配置管理主题常被学术界所忽略,但是,这对于现实的web开发尤为重要.本主题并不直接 ...

  4. spring-mybatis.xml 访问html5,Spring mvc无xml配置及利用JdbcTemplate访问数据库

    项目结构 一.新建动态web项目取名HelloSpringMVC 二./WebContent/WEB-INF/lib下导入必要依赖库 commons-collections4-4.1.jar.comm ...

  5. ssm(Spring+Spring mvc+mybatis)Dao层配置sql的文件——DeptDaoMapper.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  6. Spring IoC — 基于XML的配置

    1.属性注入 注意点: 1)如果类中显示定义了一个带参的构造函数,则一定还要显示提供一个无参构造函数,否则使用属性注入时将抛出异常. 2)JavaBean关于属性命名的特殊规范.Spring只会检查B ...

  7. 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean

    在 Spring 容器内拼凑 bean 叫做装配.装配 bean 的时候,你是在告诉容器,需要哪些 bean ,以及容器如何使用依赖注入将它们配合在一起. 理论上,bean 装配的信息可以从任何资源获 ...

  8. Spring MVC+Mybatis 多数据源配置

    文章来自:https://www.jianshu.com/p/fddcc1a6b2d8 1. 继承AbstractRoutingDataSource AbstractRoutingDataSource ...

  9. Spring配置文件beans.xml头部配置解释

    比如一个标准的beans.xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans x ...

最新文章

  1. mysql中pager命令妙用
  2. 删除苹果自带软件后果_删除 iPhone 自带的软件会有什么影响?
  3. jquery实现上线翻滚效果公告
  4. ASP.NET Web API的Controller是如何被创建的?
  5. 给本地Git配置账号信息
  6. psn账号 证明你不是机器人_世界上最聪明的机器人,AlphaGo智能机器人轻松击败世界围棋冠军...
  7. 一个屌丝程序猿的人生(四十九)
  8. 2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
  9. 分享 5 个实用的 Java 开源论坛系统!
  10. PHP关于VC11,VC9,VC6以及Thread Safe和Non Thread Safe版本选择的问题
  11. C标准库stdio源码分析
  12. word写文章 格式总是对不齐 一定要看 解决99%问题
  13. 网站CDN加速后对URL中?后的参数跟随问题
  14. macos同时运行多个版本php程序(nginx+php56+php72)
  15. 中国 / 省市区县 / 四级联动 / 地址选择器(京东商城地址选择)
  16. 苹果蓝牙耳机太贵了买哪个替代?苹果蓝牙耳机平替推荐
  17. 企业知识管理平台的作用及功能
  18. 第一次使用Arduino MKR WIFI 1010
  19. Power BI可视化之参数what-if
  20. 机器学习实战(3)——分类

热门文章

  1. 查看LINUX当前负载
  2. QGIS简介与源代码编译
  3. 继续C#开发or转做产品
  4. 让Windows 7揪出每一个暗中运行程序
  5. (转载)c++内存池实现 .
  6. VC++钩子DLL框架代码(MFC Extension DLL using shared MFC DLL)
  7. 数据结构源码笔记(C语言):B树的相关运算算法
  8. 深度学习中张量flatten处理(flatten,reshape,reduce)
  9. 电脑退出全屏按哪个键_电脑键盘上的F1到F12,每一个都是快捷键大家都会用到...
  10. js图片懒加载的第二种方式