SpringMVC之基于注解的MVC实现

SpringMVC之基于注解的MVC实现

1 示例1

1.1 spring-mvc.xml文件

添加DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter等相关信息。其中

        DefaultAnnotationHandlerMapping:支持通过url找到相关的action

        AnnotationMethodHandlerAdapter:支持通过url匹配action定义方法

      base-package:定义扫描的范围,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component@Controller@Service等这些注解的类,则把这些类注册为bean

<?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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
	">


	<!-- 
	DefaultAnnotationHandlerMapping:支持通过url找到相关的action
	AnnotationMethodHandlerAdapter:支持通过url匹配action定义方法 
	base-package:定 义扫描的范围,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
	-->	
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	<context:component-scan base-package="*"></context:component-scan>

</beans>
1.2 控制类

        加入@Controller@RequestMapping注解信息

@Controller  //用来声明控制器
@RequestMapping("/student")
public class StudentAction {
	public StudentAction(){
		System.out.println("---StudentAction构造方法被调用---");
	}
//访问可用student/save.action,save后边的action是根据web.xml配置来的
	
//如果要添加其它的数据到最后跳转过去的页面,可以在方法中添加ModelMap的参数,例如 : public String save(Student student,ModelMap map){
//...,通过map再存放其它的数据
   @RequestMapping(value="/save")
	public ModelAndView save(Student student){
		System.out.println("save方法注入的student对象:"+student);
		System.out.println("---调用业务逻辑进行业务处理---");
		
		//修改学生名字,跳转到下一页面时看能否显示修改后的名字
		student.setStuName("rename");		
		
		//直接使用字符串,返回视图,进行结果展现等
		return new ModelAndView("forward:/jsp/main.jsp");
	}
	
	//同一个action中可以定义多个方法,方法的返回类型也可以用String
	@RequestMapping(value="/update")
	public String update(Student student,ModelMap paramMap){
		System.out.println("update方法已注入student对象:"+student);
		System.out.println("---调用业务逻辑进行业务处理---");		
		
		paramMap.put("other","testOtherValue");
		//直接使用字符串,返回视图,进行结果展现等
		return "forward:/jsp/main.jsp";
	}
}

2 示例2(基于annotation-driven的注解)

基于上面的示例,在spring3中可以进一步简化配置,取代上面的注解方式.

   步骤如下:

1.使用上面的action类,仍然给类及方法添加@Controller(类)、@RequestMapping(类及方法)注解

2.本文件顶部添加spring mvc 命名空间的信息(可以参考org.springframework.web.servlet.config包)

3.添加下面注解驱动<mvc:annotation-driven>,取代了上面的DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter,并启动了json的注解

2.1 spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
	">
<!-- 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	<context:component-scan base-package="*"></context:component-scan>
	-->	
		
	<!-- mvc:annotation-driven,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
两个Bean的配置	-->
	<mvc:annotation-driven></mvc:annotation-driven>
	<context:component-scan base-package="*"/>

</beans>

3 @SessionAttributes与model.addAttribute使用

3.1 modelMap属性注入到Session

        如果希望在多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样 ModelMap 的属性才可以被跨请求访问;

@Controller
@RequestMapping("/student")
//下边如有多个属性可以用 @SessionAttributes({“attr1”,”attr2”})。
@SessionAttributes("user") 
public class StudentAction {
	public StudentAction(){
		System.out.println("---StudentAction构造方法被调用---");
	}

	@RequestMapping(value="/save")
	public String save(Student student,ModelMap map){
		System.out.println("---调用业务逻辑进行业务处理---");
		
		Student s2=new Student();
		s2.setStuAge(11);
		s2.setStuId(11111);
		map.addAttribute("user", s2);//属性名必须与session一致
		//map.addAttribute("stu", student);	
		
		//直接使用字符串,返回视图,进行结果展现等
		return "forward:/jsp/main.jsp";
	}
}
3.2 session属性注入到ModelMap

    在参数中使用@ModelAttribute("user"),可以获取@SessionAttributes("user")值

@Controller
@RequestMapping("/student")
//下边如有多个属性可以用 @SessionAttributes({“attr1”,”attr2”})。
@SessionAttributes("user") 
public class StudentAction {
	public StudentAction(){
		System.out.println("---StudentAction构造方法被调用---");
	}

	@RequestMapping(value="/save")
	public String save(Student student,ModelMap map){
		System.out.println("---调用业务逻辑进行业务处理---");
		
		Student s2=new Student();
		s2.setStuAge(11);
		s2.setStuId(11111);
		s2.setStuName("testname");
		map.addAttribute("user", s2);
		//map.addAttribute("stu", student);	
		
		//直接使用字符串,返回视图,进行结果展现等
		return "forward:/jsp/main.jsp";
	}
	
	//同一个action中可以定义多个方法
	@RequestMapping(value="/update")
	public String update(@ModelAttribute("user")Student student){
		System.out.println("update方法已注入student对象:"+student);
		System.out.println("---调用业务逻辑进行业务处理---");		
		//直接使用字符串,返回视图,进行结果展现等
		return "forward:/jsp/main.jsp";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要激活基于注解的配置方式,需要进行以下步骤: 1. 在Spring配置文件中添加以下内容: `<mvc:annotation-driven/>` 这个配置会启用Spring MVC注解驱动,使得可以使用注解来配置控制器和请求处理方法。 2. 在控制器类上添加`@Controller`注解,将该类标识为一个控制器。 3. 在请求处理方法上添加`@RequestMapping`注解,用于指定请求的路径。 例如,我们可以定义一个控制器类`ExampleController`,并配置请求的路径为`example`: ```java @Controller @RequestMapping("example") public class ExampleController { // 请求处理方法 } ``` 这样,当访问`http://localhost:8080/项目名/example`时,就会调用`ExampleController`中对应的请求处理方法。 引用\[1\]和\[2\]提供了具体的代码示例,可以参考其中的内容进行配置。 #### 引用[.reference_title] - *1* [springmvc 基于注解的配置说明](https://blog.csdn.net/han1396735592/article/details/100040564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringMVC_基于注解开发](https://blog.csdn.net/guoguo0717/article/details/110225495)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值