SpringMvc Controller 几种配置

第一种:通过URL对应bean

<!-- 配置handlerMapper  映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
<!-- 配置请求处理器 -->
<bean name="/hello.do" class="cn.controller.HelloController"/>

以上配置,访问hello.do就会找到ID为hello.do对应的bean。此配置仅适用于小型应用系统。

<!-- HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 倘若配置了HandlerAdapter;请求处理器name省去.do后缀也可以 -->
<!-- 配置请求处理器 -->
<bean name="/hello" class="cn.controller.HelloController"/>

 

 

第二种:为URL分配bean

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
		 <property name="mappings">  
			  <props>  
			  	 <!-- key 对应url   value对应 自定义控制器的id-->
			   	<prop key="/hello.do">helloController</prop>  
			  </props>  
		 </property>  
	</bean>  
	<bean id="helloController" class="cn.controller.HelloController"/>

 

 

 

第三种:URL匹配bean

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- 请求为hello*.do都可以匹配 -->
<bean id="helloController" class="cn.controller.HelloController"/>

这种方法最少用。

 

第四种:注解

<!-- spring容器扫描指定包下的所有类,如果类上有注解  那么根据注解产生相应bean对象已经映射信息 -->
<context:component-scan base-package="cn.controller"/>

 

//使用注解开发 spring mvc
//使用该注解 spring容器将会根据注解创建一个bean对象  该对象的id为类名(首字母小写)
@Controller
public class HelloController {
	//通过使用@RequestMapping 注解 可以指定请求的url被 映射到该方法上
	@RequestMapping(value="/hello.do")
	public ModelAndView hello(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hello");
		mv.addObject("msg", "annotation ------");
		return mv;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineerForSoul

你的鼓励是我孜孜不倦的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值