【问题标题】:Is it possible to reuse the pre-defined controllers in spring annotation configured mvc projects?是否可以在 spring annotation 配置的 mvc 项目中重用预定义的控制器?
【发布时间】:2010-01-07 05:05:03
【问题描述】:

是否可以重用预定义的控制器,如 SimpleFormController,带有注解配置的 spring mvc 项目?

我下载了一个看起来像我想要的项目;但是,作者没有使用 Spring 中预定义的注解。

下载项目的控制器:

package net.sourceforge.sannotations.example2;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.sannotations.annotation.Bean;
import net.sourceforge.sannotations.annotation.UrlMapping;

/**
 * User: Urubatan Date: 24/10/2006 Time: 08:49:09
 */
@Bean
@UrlMapping("/test")
public class ExampleController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("example", "message", request.getParameter("tainted"));
    }
}

项目从SourceForge下载

【问题讨论】:

    标签: java spring spring-mvc annotations


    【解决方案1】:

    您可以轻松地将它们作为 spring-context 的一部分失败 - 只需使用 @Component 注释 @Bean 注释,spring 就会将其检测为 spring bean。喜欢:

     @Component
     public @interface Bean { .. }
    

    但是,对于 URL 映射,您可能希望使用自定义 BeanPostProcessor 来处理注释的值。

    【讨论】:

    • 我明白了。所以这只能通过自定义注释来实现,对吧?使用预定义的注释无法实现这一点。
    • 不,你必须以某种方式指导 spring 如何识别自定义注释。
    猜你喜欢
    • 2021-09-08
    • 2011-12-19
    • 2015-10-11
    • 1970-01-01
    • 2020-07-20
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多