0

デフォルトのアノテーション マッピングで Spring 3.2.2 Web MVC を使用しています。次のようなサーブレット マッピングを使用します。

<servlet>
    <servlet-name>profil</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>de.kicktipp.web.config.servlets.ProfilServletConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>profil</servlet-name>
    <url-pattern>/info/profil/*</url-pattern>
</servlet-mapping>

これがサーブレット構成です。

@Configuration
@ComponentScan("de.kicktipp.controller")
@EnableWebMvc
public class ProfilServletConfig extends WebMvcConfigurerAdapter
{
    @Override
    public void addInterceptors ( InterceptorRegistry registry )
    {
       // we add a few interceptors here
    }

    @Bean
    public DefaultRequestToViewNameTranslator viewNameTranslator ( )
    {
        DefaultRequestToViewNameTranslator defaultRequestToViewNameTranslator = new DefaultRequestToViewNameTranslator();
        defaultRequestToViewNameTranslator.setStripExtension(false);
        defaultRequestToViewNameTranslator.setAlwaysUseFullPath(false);
        defaultRequestToViewNameTranslator.setPrefix("profil/");
        return defaultRequestToViewNameTranslator;
    }
}

のようなこのパターンで多くの URL を一致させたいため、ワイルドカード マッチングは重要/info/profil/page1です/info/profil/page2

末尾のスラッシュなしで「ベース」URL を一致させたい場合/info/profilは、サーブレット「プロファイル」によって取得されます。

/info/profilここで、ハンドラー メソッドと一致する 3 つのコントローラー メソッドを試しました。

@RequestMapping("/")
protected void get1 () {}

@RequestMapping("")
protected void get2 () {}

@RequestMapping("/info/profil")
protected void get3 () {}

最後の 1 つだけが機能します。これは、サーブレット内のパスが空の文字列の場合、UrlPathHelper#getLookupPathForRequest(javax.servlet.http.HttpServletRequest) がアプリケーション内のフル パスを返すためです。

public String getLookupPathForRequest(HttpServletRequest request) {
    // Always use full path within current servlet context?
    if (this.alwaysUseFullPath) {
        return getPathWithinApplication(request);
    }
    // Else, use path within current servlet mapping if applicable
    String rest = getPathWithinServletMapping(request);
    if (!"".equals(rest)) {
        return rest;
    }
    else {
        return getPathWithinApplication(request);
    }
}

"/info/profil/" へのリクエストの場合、メソッドは "/" を返しますが、"/info/profil" (末尾のスラッシュなし) の場合は "/info/profil" を返します。これは、残りの変数が空の文字列であり、前にあるためです。メソッドは pathWithinApplication を返します。

他のパスは通常、サーブレット マッピング内のパスと照合されます (alwaysUseFullPath のデフォルトは false であるため)。ただし、「ルート」パスは、アプリケーション内のフル パスと照合されます (alwaysUseFullPath が true の場合に常に行われるように)。

なぜこのようになっているのですか?spring が空の文字列と一致させようとせず、代わりにアプリケーション内のパスと一致させようとするのはなぜですか?

ここで春の問題を参照してください https://jira.springsource.org/browse/SPR-10491

4

2 に答える 2

3

追加してみてください

@RequestMapping(method = RequestMethod.GET) public String list() { return "redirect:/strategy/list"; }

結果:

    @RequestMapping(value = "/strategy")
    public class StrategyController {
    static Logger logger = LoggerFactory.getLogger(StrategyController.class);

    @Autowired
    private StrategyService strategyService;

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "redirect:/strategy/list";
    }   

    @RequestMapping(value = {"/", "/list"}, method = RequestMethod.GET)
    public String listOfStrategies(Model model) {
        logger.info("IN: Strategy/list-GET");

        List<Strategy> strategies = strategyService.getStrategies();
        model.addAttribute("strategies", strategies);

        // if there was an error in /add, we do not want to overwrite
        // the existing strategy object containing the errors.
        if (!model.containsAttribute("strategy")) {
            logger.info("Adding Strategy object to model");
            Strategy strategy = new Strategy();
            model.addAttribute("strategy", strategy);
        }
        return "strategy-list";
    }  

** クレジット:

高度な @RequestMapping トリック – コントローラー ルートと URI テンプレート

dtr-trading.blogspot.it による CRUD チュートリアル

于 2014-05-14T14:10:59.130 に答える