maven与springMVC之Controller

还记得上篇文章中的有关controller的配置吗:
<context:component-scan base-package="com.wangpeng.testmodule.controller" />
<context:component-scan/> 用来表明扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件
@Service    声明Service组件    @Service("myMovieLister") 
@Repository 声明Dao组件
@Component   泛指组件, 当不好归类时. 
@RequestMapping  请求映射
@Resource  用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") 
@Autowired 用于注入,(srping提供的) 默认按类型装配 
@Transactional( rollbackFor={Exception.class}) 事务管理
@ResponseBody

@Scope("prototype")   设定bean的作用域

md_index

作为一个controller类首先需要通过@Controller,但是只用@Controller 声明是没有意义的,我们还要通过@RequestMapping来设定请求映射,否则没法访问。需要注意的是,此例返回的是Map,并且传了一个参数message=md_index,在jsp中可以使用el表达式获取${message}。

@Controller
//@RequestMapping("/testControl")
public class SpringController {
	
	@RequestMapping("md_index")
	public Map<String, String> testMdIndex() {
		System.out.println("testControl/md_index-------------->");
		Map<String, String> map = new HashMap<String, String>();
		map.put("message", "md_index");
		return map;
	}
}
如上代码,SpringController 类由@Controller 声明,这样分发处理器将会扫描SpringController类和它的方法,并检测是否使用了@RequestMapping 注解。并且我给方法添加了@RequestMapping("md_index")标记,定义它的请求名为md_index。

在此实例中我把@RequestMapping("/testControl")给注掉了,主要是因为testMdIndex方法返回的是Map,这种情况下Controller返回的视图是和@RequestMapping定义的名称md_index同名的jsp,并且此jsp前缀是和spring-mvc.xml中InternalResourceViewResolver定义的是一样的。

也就是说,上面代码对应的:

请求路径是:http://127.0.0.1:8080/test_web/md_index

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值