springMVC学习(基于注解的MVC)

来源:互联网 发布:邬贺铨 大数据 编辑:程序博客网 时间:2024/06/04 01:01

今天这篇博客给大家带来基于注解的springMVC开发,大家之前有没有想过一个问题,就是我们之前基于配置文件配置写出来的那些Controller只能默认处理一个方法,那么我如果想像structs2那样子来让一个Controller可以处理多个请求,对于不同的请求,跳转进不同的方法来做处理,这样是极好的,springMVC基于注解的开发,就可以完成上边的这些需求,下面我们看一个例子。

添加jar文件

首先我们需要添加需要的jar文件,这里只是比上一篇springMVC helloworld多了一个com.springsource.javax.annotation-1.0.0.jar,如下:
com.springsource.javax.annotation-1.0.0.jar
com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar

配置spring的核心分发器

接下来需要配置我们的spring核心分发器,在web.xml中添加如下内容:

<servlet>    <servlet-name>spring</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-servlet.xml</param-value>        </init-param></servlet><servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>/</url-pattern> </servlet-mapping>

这里我将核心分发器的名称改为spring,同时初始化参数contextConfigLocation在类路径下,因此我们需要在src下创建一个名称为:spring-servlet.xml的文件

创建spring-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                         http://www.springframework.org/schema/mvc                         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd                         http://www.springframework.org/schema/context                         http://www.springframework.org/schema/context/spring-context-3.0.xsd                         http://www.springframework.org/schema/aop                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                         http://www.springframework.org/schema/tx                         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">    <mvc:annotation-driven/>    <context:component-scan base-package="com.test.spring.zhujie"></context:component-scan>    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/pages/"></property>        <property name="suffix" value=".jsp"></property>    </bean></beans>

这里与上一篇博客不同的是,添加了一个“注解驱动”和”注解驱动扫描的包”,即在这里配置了以后,该包下的注解才会生效。

<mvc:annotation-driven/>    <context:component-scan base-package="com.test.spring.zhujie">    </context:component-scan>

关于ViewResolver的配置是相同的,这里就不多说了哈。
下面我新建一个Indexontroller,并且加上注解:

package com.test.spring.zhujie;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping(value="/index")public class IndexController {    @RequestMapping(value="/add")    public String add() {        System.out.println("add runs ....");        return "index";    }}

@Controller用来标注该类是一个Controller,所以不用像之前那样继承自各种Controller,@RequestMapping(value=”/index”)表示我可以通过”/index”这样的url访问到该类。在看看add()方法,@RequestMapping(value=”/add”)表示子路径,也就是如果想要跳转到该方法来处理的话,必须加上根路径,比如这里我想要让add方法来做处理,url可以这样写”http://loalhost:8080/springmvc2/index/add“,这样就会交给该方法来处理了。
在add方法中,我们返回的是”index”,结合配置的ViewResolver,这个返回值将会跳转到”WEB-INF/pages/index.jsp” ,我们可以发现,基于注解的配置,可以省去不少代码,而且一个controller类当中,可以处理多个方法。

那么如果我需要传递参数怎么办的??这里我在写一个方法,该方法接收一个int类型的id。

@RequestMapping(value="remove")    public String remove(int id) {        System.out.println("remove runs .... id is :"+id);        return "remove";}

此时,我通过该url访问,就可以直接得到传递过来的参数:
“http://localhost:8080/springmvc2/index/remove?id=3”
可以发现,只要我声明的参数名成和传递的参数名称是相同的,就可以获得。

如果我有很多个参数,怎么办??难道还在方法中声明很多个参数吗??这里我在新建一个方法:

@RequestMapping(value="delete")public String delete(Person person) {        System.out.println("the person is :"+person);        return "delete";}

可以看到我在方法中声明了一个Person类型的参数,然后只需要通过这样的url来访问,就可以得到浏览器端传递过来的参数。
http://localhost:8080/springmvc2/index/delete?id=2&name=uuu&pass=iii
需要注意,这里传递的参数名称需要和Person类中声明的属性一样。

还有另外一种传统方式获取参数,就是在方法中声明一个HttpServletRequest类型的参数。这里我在定义一个方法:

@RequestMapping(value="update")public String update(HttpServletRequest request) {        String param = request.getParameter("param");        System.out.println("the param is :"+param);        return "update";}

可以看到这里可以直接通过声明的request参数获取到传递过来的参数,这个时候,就可以通过这样的url来传递参数:http://localhost:8080/springmvc2/index/update?param=88

那么如果我只想让我的方法处理get请求,该怎么写呢??
只需要这样写就可以了.
@RequestMapping(value=”add”,method=RequestMethod.GET)

好了,今天的springMVC基于注解的实现,就到这里了。
源码下载

0 0
原创粉丝点击