【Java EE 学习 83 下】【SpringMVC】【使用注解替代已过期的API】【SpringMVC、Hibernate整合】

1、SpringMVC中注解的使用

  1.为何要使用注解

    以前曾经提到过的三种控制器在spring3.0中都已经被明确标记为过期了,spring3.0推荐使用注解的方式替代三种控制器,实际上使用注解的方式可以大大提升开发效率。前端

  2.使用注解@RequestMapping

    使用注解须要对配置文件进行改动:java

  (1)spring配置文件的改动

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/mvc 
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
10         http://www.springframework.org/schema/beans 
11         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
12         http://www.springframework.org/schema/context 
13         http://www.springframework.org/schema/context/spring-context-3.2.xsd
14         http://www.springframework.org/schema/aop 
15         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
16     <!-- 注解驱动 -->
17     <mvc:annotation-driven />
18     <!-- 扫描组件 -->
19     <context:component-scan base-package="com.kdyzm.controller"></context:component-scan>
20     <!-- 配置内部视图资源解析器 -->        
21     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22         <property name="prefix" value="/"></property>
23         <property name="suffix" value=".jsp"></property>
24     </bean>        
25 </beans>

    如上所示,须要增长注解驱动,同时须要进行包扫描。mysql

  (2)@RequstMapping注解

    该注解是SpringMVC中的核心注解,它直接加到类和方法上;加到类名上表明父路径,加到方法上表明子路径,访问的方法就是父路径+子路径,以下所示:git

@Controller
@RequestMapping(value="/person")
public class PersonController {
    @Resource(name="personService")
    private PersonService personService;
   @RequestMapping(value="/save")
  public String savePerson(Person p){ this.personService.saveEntity(p); return "redirect:/person/listAll"; } }

    第一个注解加到了类名上:/person,第二个注解加到了方法上:/save,那么访问该控制器的方法就是:http://localhost:8080/springmvc2/person/savegithub

2、使用注解接收参数的方法

  直接将须要的参数放到方法参数列表中,而后在方法中直接使用便可;能够传递的参数类型有:Seriable、自定义对象、HttpServletRequest、Model等,放到了方法的参数列表中的参数会自动进行识别并封装。web

  如对于一个表单提交的数据,能够直接在控制器方法中定义一个对象实体的参数,这样数据就会自动封装到对象中了;也能够只放置须要的参数类型,好比public updatePerson(String id),这样前端传递过来的id的值就直接拿过来了;若是参数类型是HttpServletRequest,那么可使用该对象获取各类请求参数,入public String updatePerson(HttpServletRequest request);若是参数是Model类型的,那么实际上就将全部的请求都放到了一个Map对象中,经过Model对象的asMap方法就可以获取该Map对象,接下来直接使用key值获取对应的请求值就能够了。spring

  四种获取参数的方法以下代码所示:sql

@RequestMapping(value="updatePersonInf")
public String updateMethod(String id,Person person,HttpServletRequest request,Mode model){
  System.out.println(id);
  System.out.println(person.getId());
  System.out.println(request.getParameter("id"));
  System.out.println(model.asMap().get("id"));    
  return "redirect:/person/listAll"; }

3、重定向的方法

  SpringMVC中默认使用转发跳转到目标页,若是须要重定向的话,则使用redirect:前缀修饰,如上代码所示。express

4、SpringMVC和Hibernate的整合

  1.首先是使用到的全部jar包:

antlr-2.7.6.jar
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
c3p0-0.9.5.jar
commons-collections-3.1.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jstl.jar
jta-1.1.jar
junit.jar
log4j-1.2.15.jar
mchange-commons-java-0.2.9.jar
mysql-connector-java-5.1.10-bin.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-context-support-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
spring-webmvc-portlet-3.2.0.RELEASE.jar
standard.jarapi

  2.整合说明:

  整合SpringMVC和Hibernate,因为SpringMVC也是Spring的一部分,因此也能够说成第二种"SSH(SpringMVC、Spring、Hibernate)"

  Spring版本:spring3.2.0

  Hibernate版本:hibernate3.5.6

  mysql版本:5.5.25

  Eclipse版本:Version: Mars Release (4.5.0)

  3.因为比较简单,因此不作赘述了,只是简单作了对Person对象的CRUD,以下图所示:

  

  4.项目练习源代码地址

  https://github.com/kdyzm/springmvc2_ssh

相关文章
相关标签/搜索