没有注释的 Spring MVC 3.1? [英] Spring MVC 3.1 without annotations?

查看:35
本文介绍了没有注释的 Spring MVC 3.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 使用注解;我更喜欢使用 SimpleUrlHandlerMapping 将 Web 应用程序的所有 URL 集中在一个地方.

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 的许多工作中,我非常习惯 BaseCommandController 层次结构.

Also, from much previous work using Spring 2.x, I'm very used to the BaseCommandController heirarchy.

我一直很喜欢 Spring,因为它可以赋予权力而不是限制.现在我发现 Spring MVC 迫使我将 URL 放入 java 源代码中,这意味着 (a) 我无法将控制器映射到多个 URL,以及 (b) 要发现 webapp 中正在使用的 URL,我必须扫描通过不同的 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() {

如果这行得通,那很好.

If this works then good.

我的问题仍然存在:如果我不想在我的 java 源代码中使用 URL,如何最好地将它们与 @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.

Dispatcher 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;
    }
}

mvc annotation-config 的控制器

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 注释使用你自己的处理程序.您可以查看 ClassPathBeanDefinitionScannerDefaultAnnotationHandlerMapping.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆