简体   繁体   English

如何在SpringBoot中使用SimpleUrlHandlerMapping

[英]How to Use SimpleUrlHandlerMapping with SpringBoot

I am using SpringBoot and want to configure SimpleUrlHandlerMapping bean for my custom mapping. 我正在使用SpringBoot,并想为我的自定义映射配置SimpleUrlHandlerMapping bean。 For that follow are the piece of code that I wrote. 为此,下面是我编写的代码。

@Configuration
public class WebConfiguration {

    @Bean
    public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
        System.out.println("creating  SimpleUrlHandlerMapping ....");
        SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
        simpleUrlHandlerMapping.setOrder(0);
        Properties urlProperties = new Properties();
        urlProperties.put("/index", "myController");

        simpleUrlHandlerMapping.setMappings(urlProperties);

        return simpleUrlHandlerMapping;
    }
}

I also have one Controller with name myController and its code looks like this. 我还有一个名称为myController的Controller,其代码如下所示。

@Controller("myController")
public class MyController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        System.out.println("My Controller!");
        return null;
    }

}

Now as per code when http://localhost:7171//index is hit then it should print the My Controller message on console. 现在按照代码,当点击http:// localhost:7171 // index时,它将在控制台上打印My Controller消息。 But it not touch this code. 但是它不碰这个代码。 Because this is an SpringBoot application and on start it print this bean registration with myController. 因为这是一个SpringBoot应用程序,并且在启动时使用myController打印此bean注册。

Could someone help to resolve this issue and tell me whats wrong in this code. 有人可以帮助解决此问题,并告诉我这段代码有什么问题。

Thanks in advance. 提前致谢。

@Autowire Controller Bean in Configuration class and pass it through Properties 配置类中的@Autowire Controller Bean并将其通过Properties传递

SimpleUrlHandlerMapping is the most flexible HandlerMapping implementation. SimpleUrlHandlerMapping是最灵活的HandlerMapping实现。 It allows for direct and declarative mapping between either bean instances and URLs or between bean names and URLs. 它允许在bean实例和URL之间或bean名称和URL之间进行直接和声明性映射。

Let's map requests “/simpleUrlWelcome” and “/*/simpleUrlWelcome” to the “welcome” bean: here 让我们将请求“ / simpleUrlWelcome”和“ / * / simpleUrlWelcome”映射到“ welcome” bean: 这里

@Configuration
public class WebConfiguration {

@Autowired
private indexController index;

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
    System.out.println("creating  SimpleUrlHandlerMapping ....");
    SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
    simpleUrlHandlerMapping.setOrder(0);
    Properties<String,Object> urlProperties = new Properties<>();
    urlProperties.put("/index", index);

    simpleUrlHandlerMapping.setMappings(urlProperties);

    return simpleUrlHandlerMapping;
     }
 }

Controller 调节器

@Controller("index")
public class indexController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    System.out.println("My Controller index!");
    return null;
     }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM