groovy编写spring的controller(四)支持annotation

groovy编写spring的controller(四)支持annotation

前面已经准备好了groovyplugin-1.0.jar了,那么接着就应该在easygroovy这个项目中使用这个
pom.xml中引入的JAR包,这里列个清单
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.7-beta-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.sillycat.easygroovyplugin</groupId>
<artifactId>easygroovyplugin</artifactId>
<version>1.0</version>
</dependency>

manager层模拟的代码基本上没有更新,groovy文件的配置也基本上没有修改,修改比较大的是
applicationContext.xml引入了新的xsd。哈哈。就是我们的easygroovyplugin-1.0.jar里面的groovy.xsd,文件如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:groovy="http://www.sillycat.com/schema/groovy"
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/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.sillycat.com/schema/groovy http://www.sillycat.com/schema/groovy/groovy.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
   lazy-init="false">
   <property name="locations">
    <list>
     <value>classpath*:easygroovy.properties
     </value>
    </list>
   </property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/jsp/" />
   <property name="suffix" value=".jsp" />
</bean>
    <context:component-scan base-package="com.sillycat.easygroovy.web" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean id="handlerAdapter" class="com.sillycat.easygroovyplugin.handlers.ProxyAwareAnnotationMethodHandlerAdapter" />
<lang:defaults refresh-check-delay="5" />
<groovy:scan source-pattern="/groovy/*.groovy" />
<bean id="groovyManager" class="com.sillycat.easygroovy.service.impl.GroovyManagerImpl"/>
</beans>
注意里面的http://www.sillycat.com/schema/groovy就是用到的我们自定义的东西啦。
我们的GroovyController.groovy也做了annotation的配置,文件如下:
package com.sillycat.easygroovy.web;

import com.sillycat.easygroovy.model.User;
import com.sillycat.easygroovy.service.GroovyManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/groovy.do")
class GroovyController {
@Autowired
GroovyManager groovyManager
@RequestMapping(params = "method=welcome" ,method = RequestMethod.GET)
    public ModelAndView welcome() throws IOException {
   System.out.println("cccccccccc1")
   groovyManager.echo()
   User user = groovyManager.get(null)
   return new ModelAndView("welcome","user",user)
    }
}
class上面的@RequestMapping("/groovy.do")表示访问到groovy.do就访问到这个controller了。
后面方法上的@RequestMapping(params = "method=welcome" ,method = RequestMethod.GET)
表示,URL上有GET传递过来的参数method=welcome的话,那么就会进入到这个方法。所以index.jsp上测试文件如下:
<html>
<body>
<h1>Home Page</h1>
<p>
Anyone can view this page.
</p>
<a href="groovy.do?method=welcome">welcome</a>
</body>
</html>

你可能感兴趣的:(spring,log4j,Web,servlet,groovy)