java之spring mvc之Controller配置的几种方式
这篇主要讲解 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框架学习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现