我曾見過this問題,我無法在我的Spring MVC項目中使用Groovy。我應該在哪裏保存它,以及應該是什麼樣的完整路徑?請原諒我的基本問題,但我必須開始在Spring MVC中使用Groovy。 Groovy在Spring 4.0 Snapshot中可用,根據官方網站,最終版本應該在12月份上線。但是現在,最好使用4.0版本嗎? (它已得到內建支持Groovy中)我應該如何在Spring MVC中創建Groovy控制器
引用代碼:
<beans .........
...
...
<bean class="full.qualified.name.of.ProxyAwareAnnotationMethodHandlerAdapter" />
...
...
<lang:groovy script-source="classpath:com/example/mysample.groovy refresh-check-delay="1000" />
</beans>
和Java類下面是:
//ProxyAwareAnnotationMethodHandlerAdapter.java
package name.assafberg.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
/**
* Add proxy awareness to <code>AnnotationMethodHandlerAdapter</code>.
*
* @author assaf
*/
public class ProxyAwareAnnotationMethodHandlerAdapter extends AnnotationMethodHandlerAdapter {
/**
* @param request
* @param response
* @param handler
* @return
* @throws Exception
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
*/
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
handler = unwrapHandler(handler);
return super.handle(request, response, handler);
}
/**
* @param handler
* @return
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#supports(java.lang.Object)
*/
@Override
public boolean supports(Object handler) {
handler = unwrapHandler(handler);
return super.supports(handler);
}
/**
* Attempt to unwrap the given handler in case it is an AOP proxy
*
* @param handler
* @return Object
*/
private Object unwrapHandler(Object handler) {
if (handler instanceof Advised) {
try {
TargetSource targetSource = ((Advised) handler).getTargetSource();
return targetSource.getTarget();
} catch (Exception x) {
throw new RuntimeException(x);
}
} else {
return handler;
}
}
}
編輯
沒有必要處理與上述文件。唯一的事情是我要照顧Groovy的版本。請確保從命令行,該版本的Groovy與由Class路徑添加的版本相同。它的工作。幾乎沒有任何配置要求。剛剛創建了一個.groovy作爲類而不是Java一樣(感謝+的啓發KUNAL。)
@Controller
class TestController {
@Autowired
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
String People() {
return "people"
}
}
有點晚了...但我試圖在我的項目中使用Groovy控制器。我跟着這裏的文檔:http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven 而且不得不改變常規版本到2.1.6 我不明白你的Autowired對人的方法 –