这篇主要讲解 controller配置的几种方式。

1. URL对应 Bean

如果要使用此类配置方式,需要在XML中做如下样式配置

<!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置Controller -->
<bean name="/hello.do" class="cn.sxt.controller.HelloController"/>

2. 为 URL 分配 Bean

使用一个统一配置集合,对各个 URL 对应的 Controller 做关系映射

<!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="cn.sxt.controller.HelloController"></bean>

该配置可以使用通配符

3. URL 匹配 Bean

如果定义的 Controller 名称规范,也可以使用如下配置

将 hello*.do 交给 helloController 处理

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<bean id="helloController" class="cn.sxt.controller.HelloController"></bean>

4.使用注解进行开发

需要导入 aop.jar 包

Controller 的 开发:

/**
* @Controller 注解一个控制器 需要扫描
*/
@Controller
public class HelloController{
/**
* 注解请求的url
*/
@RequestMapping("/hello.do")
public ModelAndView hello(HttpServletRequest req){
System.out.println("使用注解进行开发:"+req.getRemoteHost());
ModelAndView mv = new ModelAndView("hello");
mv.addObject("msg", "使用注解开发Controller");
return mv;
}
}

配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解开发适配器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 扫描注解类 -->
<context:component-scan base-package="cn.sxt.controller"/>
</beans>

附录:

这里附上上面配置的完整配置信息

附一

<?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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 配置handlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置Controller -->
<bean name="/hello.do" class="cn.vincent.controller.HelloController"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

附二

<?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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置handlerMapping -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">helloController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="cn.sxt.controller.HelloController"></bean>
<!-- 配置handlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

附三

<?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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
<bean id="helloController" class="cn.sxt.controller.HelloController"></bean>
<!-- 配置handlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>

附四

<?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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解开发适配器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 为响应的视图名称加上前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 为响应的视图名称加上后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 扫描注解类 -->
<context:component-scan base-package="cn.sxt.controller"/>
</beans>

java之spring mvc之Controller配置的几种方式的更多相关文章

  1. spring mvc获取路径参数的几种方式 - 浅夏的个人空间 - 开源中国社区

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  2. Spring MVC中forward请求转发2种方式(带参数)

    Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html  

  3. Spring MVC异常统一处理的三种方式

    Spring 统一异常处理有 3 种方式,分别为: 使用 @ ExceptionHandler 注解 实现 HandlerExceptionResolver 接口 使用 @controlleradvi ...

  4. spring mvc获取路径参数的几种方式

    一.从视图向controller传递值,  controller <--- 视图 1.通过@PathVariabl注解获取路径中传递参数 (参数会被复制到路径变量) @RequestMappin ...

  5. Spring管理 hibernate 事务配置的五种方式

    Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...

  6. spring mvc 返回json数据的四种方式

    一.返回ModelAndView,其中包含map集 /* * 返回ModelAndView类型的结果 * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式 ...

  7. Java spring mvc多数据源配置

    1.首先配置两个数据库<bean id="dataSourceA" class="org.apache.commons.dbcp.BasicDataSource&q ...

  8. spring mvc在Controller中获取ApplicationContext

    spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...

  9. Java之Spring mvc详解

    文章大纲 一.Spring mvc介绍二.Spring mvc代码实战三.项目源码下载四.参考文章   一.Spring mvc介绍 1. 什么是springmvc   springmvc是sprin ...

随机推荐

  1. Hash总结

    算法介绍 这个没什么好说的,就是一段比较简单的代码,具体的话要看题目. 自然溢出 这个是比较好用而且容易被卡的一种Hash方式. 用\(2^{64}\)当模数然后做\(Hash\),常数比较小. 单模 ...

  2. java的static和this

    1>static:静态修饰符   static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念.    被sta ...

  3. java并发编程(八) CAS & Unsafe & atomic

    参考文档:https://www.cnblogs.com/xrq730/p/4976007.html CAS(Compare and Swap) 一个CAS方法包含三个参数CAS(V,E,N).V表示 ...

  4. mysql根据关键词查询匹配多个字段时结果不正确

    一开始的写法 SELECT rrc.id, rrc.resource_name rrc.is_publish FROM res_resource_catalog AS rrc <where> ...

  5. JVM探究之 —— 类文件结构(脑图)

  6. 利用FUSE编写自定义的文件系统

    FUSE--用户空间文件系统(Filesystem in Userspace),具体可以度娘,反正是简化了自定义文件系统的复杂度,可以更方便地利用自定义文件系统做一些事情. 一.使用 Python 编 ...

  7. CentOS 7.5 安装Oracle 11gR2 86%报错:Error in invoking target 'agent nmhs' of makefile

    解决方案: 不要关闭安装过程,另外打开终端窗口,将ins_emagent.mk文件中的 (MK_EMAGENT_NMECTL)更改为$(MK_EMAGENT_NMECTL) -lnnz11,然后在安装 ...

  8. 最新sublime3配C++11编译环境

    sublime text 3. version 3.2.1 build 3207 Package control: install package : C++11 (v0.0.9; github.co ...

  9. python初级(302) 6 对象(三)

    一.复习 1.什么是魔法方法? 2.什么是类的初始化函数? 二.什么是self 使用一个类可以创建多个对象实例,例如: ball1 = Ball("red", "smal ...

  10. Egret HTML5游戏开发指南

    Egret  HTML5游戏开发指南 下载地址:https://pan.baidu.com/s/1fuxllvmRhWXoWDwH4gxN9g 关注微信公众号获取提取码: 输入:egrt 获取提取码