java mvc controller_【java框架】SpringMVC(2)--SpringMVC实现Controller方式

1.   SpringMVC的Controller实现方式

SpringMVC实现Controller的方式主要有控制器实现方式与全注解实现方式,其中全注解实现方式是当前项目中比较常用的一种方式。

1.1.控制器实现方式

1.1.1.     实现Controller接口

创建一个类实现Controller接口:

/*** 实现Controller方式一:

* 实现一个Controller接口,实现handleRequest方法

* 并且在Springmvc的配置文件中配置这个bean,指定Demo1Controller的访问路径*/

public class Demo1Controller implementsController {

@Overridepublic ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throwsException {

System.out.println("进入demo1Controller视图Model实现方式...");//创建ModelAndView对象

ModelAndView modelAndView = newModelAndView();

modelAndView.setViewName("/WEB-INF/views/demo1Controller.jsp");returnmodelAndView;

}

}

配置applicationContext-mvc.xml文件,配置bean交给Spring来管理:

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

1.1.2.     实现HttpRequestHandler接口

创建一个类来实现HttpRequestHandler接口,实现handleRequest方法:

/*** 实现Controller方式二:

* 实现一个HttpRequestHandler接口,实现handleRequest方法

* 并且在Springmvc的配置文件中配置这个bean,指定Demo2Controller的访问路径*/

public class Demo2Controller implementsHttpRequestHandler {

@Overridepublic void handleRequest(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

System.out.println("进入demo2Controller视图Model实现方式...");//获取参数//request.getParameter("name");//转发

request.getRequestDispatcher("/WEB-INF/views/demo2Controller.jsp").forward(request, response);

}

}

配置applicationContext-mvc.xml文件,配置bean交给Spring来管理:

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

1.1.3.     普通类(POJO)注解实现

创建一个类,在类中可以提供多个method方法,使用@RequestMapping映射类路径+方法路径,,这样一个类中就可以配置多个访问路径:

/*** 实现Controller方式三:

* 普通类(POJO)和注解@RequestMapping

* 配置访问路径:类路径+方法路径

* 这样一个类中可以配置多个方法、多个方法url映射

* 同样需要在applicationContext-mvc.xml中配置bean

* 而且需要开启SpringMVC的注解支持:识别并扫描类上面的注解@RequestMapping*/@RequestMapping("/demo3Controller")public classDemo3Controller {

@RequestMapping("/add")publicModelAndView add(){

System.out.println("进入demo3Controller视图Model的add方法...");

ModelAndView modelAndView= newModelAndView();

modelAndView.setViewName("/WEB-INF/views/demo3Controller_add.jsp");returnmodelAndView;

}

@RequestMapping("/del")publicModelAndView del(){

System.out.println("进入demo3Controller视图Model的del方法...");

ModelAndView modelAndView= newModelAndView();

modelAndView.setViewName("/WEB-INF/views/demo3Controller_del.jsp");returnmodelAndView;

}

}

配置applicationContext-mvc.xml文件,配置bean交给Spring来管理,无需配置name访问路径,注意:必须配置开启SpringMVC注解配置的扫描:

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

1.2.全注解实现方式

全注解实现方式较控制器实现方式简单,而且只需要我们普通的一个Controller类上面添加@Controller与@RequestMapping注解即可,是一种项目中常见的注解配置方式。

主要的步骤如下:

①    创建一个普通的类,在类上面配置@Controller、@RequestMapping注解;

/*** 实现Springmvc全注解方式:

* 写一个普通的Controller类

* 无需在applicationContext-mvc.xml中配置bean,只需要使用@Controller告诉Spring这是一个bean

* 通过@RequestMapping在类与方法上映射请求路径,可以映射多个方法的请求路径*/@Controller

@RequestMapping("/annoController")public classAnnotationController {

@RequestMapping("/add")publicModelAndView add(){

System.out.println("进入AnnotationController中的add方法...");

ModelAndView modelAndView= newModelAndView();

modelAndView.setViewName("/WEB-INF/views/annoController_add.jsp");returnmodelAndView;

}

@RequestMapping("/del")publicModelAndView del(){

System.out.println("进入AnnotationController中的add方法...");

ModelAndView modelAndView= newModelAndView();

modelAndView.setViewName("/WEB-INF/views/annoController_del.jsp");returnmodelAndView;

}

}

②    在applicationContext-mvc.xml中配置—开启对SpringMVC注解的支持、配置包的扫描(具体到哪个路径下去扫描@Controller)、兼容Spring3.2版本的配置,具体配置如下:

applicationContext-mvc.xml

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

③   注意事项:还需要加上spring-aop.jar包,否则在处理注解映射时无法找到对应切面的映射会抛出aop异常:

Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource

解决方案,导入spring-aop-4.1.2.RELEASE.jar包即可。

对应方法访问页面/annoController/add与annoController/del:

cad0564db76a71a40d5acff831415655.png

b4ceabf3ef6411ecaea779ef4b7e146e.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值