Spring MVC的3.1注释没有? [英] Spring MVC 3.1 without annotations?
问题描述
我开始使用Spring 3.1的新项目,并已眼球深处关于如何使用注解@Controller的所有文件和论坛的意见。
I'm starting a new project with Spring 3.1, and have been eyeball deep in all the documentation and forum opinions about how to use the @Controller annotation.
我个人不喜欢使用注解MVC;我有很多preFER可用的webapp所有URL在一个地方,使用SimpleUrlHandlerMapping建立。
I personally dislike using annotations for MVC; I much prefer having all the URLs of a webapp available in one place, using SimpleUrlHandlerMapping.
另外,从使用Spring 2.x的多previous的工作,我很习惯了BaseCommandController层次结构。
Also, from much previous work using Spring 2.x, I'm very used to the BaseCommandController heirarchy.
我总是喜欢春天,因为它的授权没有被限制。现在我发现Spring MVC中迫使我把URL转换成Java源代码,这意味着(一)我控制器不能映射到多个URL,以及(b)发现什么URL是在web应用程序的使用,我必须扫描通过不同的Java源文件,我觉得这是不切实际的。
I've always loved Spring because it's empowering without being restricting. Now I find Spring MVC is forcing me to put URLs into the java source, meaning (a) I can't map a controller to several URLs, and (b) to discover what URLs are in use in a webapp, I have to scan through different java source files, which I find impractical.
什么是@Controller与SimpleUrlHandlerMapping建立相结合的推荐方式,好吗?
What is the recommended way of combining @Controller with SimpleUrlHandlerMapping, please ?
更新:
戴夫你好,你说你可以映射像这样多个URL(从petclini.web.ClinicController改变)?
Hi Dave, are you saying you can map multiple URLs like this (altered from petclini.web.ClinicController)?
@RequestMapping({"/vets", "/another"})
public ModelMap vetsHandler() {
如果这工作那么好。
我的问题仍然有效,但:
如果我不希望在我的Java源代码的网址,如何最好地使用@Controller类并将它们映射?
My question still stands though: If I don't want URLs in my java source, how best to map them with @Controller classes?
问候,
推荐答案
下面是一个简单的设置来支持注释和非注释的控制器。
Here is a simple set up to support annotation and non-annotated controllers.
调度servlet配置XML
Dispatcher servlet configuration xml
<mvc:annotation-driven/>
<bean id="testController" class="com.test.web.TestController"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/test=testController
</value>
</property>
<property name="order" value="0"/>
</bean>
一个简单的URL映射到控制器
A simple URL mapped controller
public class TestController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
PrintWriter responseWriter = response.getWriter();
responseWriter.write("test");
responseWriter.flush();
responseWriter.close();
return null;
}
}
The controller for mvc annotation-config
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String dashboard(Model model, HttpServletRequest request) {
return "home";
}
}
如果你想使用自己的处理程序@Controller注解。你也许可以考虑ClassPathBeanDefinitionScanner和DefaultAnnotationHandlerMapping.determineUrlsForHandlerMethods。
If you want to use your own handlers for @Controller annotation. you can probably look into ClassPathBeanDefinitionScanner and DefaultAnnotationHandlerMapping.determineUrlsForHandlerMethods.
这篇关于Spring MVC的3.1注释没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!