风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

Spring MVC Annotation

spring mvc 使用annotation进行controller配置
注:两种urlmapping的模式不能同时使用,如果使用annotation就不能再配置springapp-servlet.xml的urlmapping。

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

    <context:component-scan base-package="com.ivo.web.action.spring" />
 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
        <property name="prefix"><value>/pages/</value></property>
        <property name="suffix"><value>.jsp</value></property>
    </bean>
   
</beans>

    <context:component-scan base-package="com.ivo.web.action.spring" />用于指明系统从哪个路径下寻找controller,然后提前初始化这些对象。

2、web.xml
    跟一般的mvc配置没有区别。

3、controller配置
    具体可以看spring的参考文件,有好几种配置。

    3.1 在方法上配置mapping路径
        

@Controller
public class HelloController {

 @RequestMapping("/hello.do")
 public ModelAndView processImageUpload() throws IOException {
  System.out.println("hello");
  return new ModelAndView("org", "jsonString", "test");
 }

}

    3.2 在class上配置mapping路径,通过参数指定调用的方法

@Controller
@RequestMapping("/hello.do")
public class HelloController {

@RequestMapping(params = "action=save")
 public ModelAndView save() throws IOException {
  System.out.println("hello");
  return new ModelAndView("org", "jsonString", "test");
 }

}

方法的返回值可以是null,string 或者ModelAndView,都支持。

以上是系统暂时用到的,其他的以后再研究。

posted on 2008-01-10 13:35 风人园 阅读(1801) 评论(0)  编辑  收藏 所属分类: Spring


只有注册用户登录后才能发表评论。


网站导航: