Configuration of spring MVC in spring boot

Posted by Firestorm ZERO on Sun, 05 Apr 2020 03:11:10 +0200

Using spring MVC in spring boot

To use spring MVC in spring boot, you only need to introduce spring boot starter web (provided that the pom file of the project is inherited from spring boot starter parent):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Then you can configure some parameters of springmvc in the application.properties file, such as the path to load jsp, etc

spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp

Let's talk about something we don't have online

If some of our variables need to be referenced in the form of ${xxx} in jsp (note that here is jsp, not freemarker, etc., freemarker's configuration mode is later), and the values of these variables are assigned in java code, we can use @ ControllerAdvice annotation, and cooperate with UrlBasedViewResolver object and @ PostConstruct, @ InitBinder and other annotations to achieve Our effect:

@ControllerAdvice
public class SpringmvcConfiguration {

    @Autowired
    UrlBasedViewResolver urlBasedViewResolver;

    @PostConstruct
    void init(){
        //Configure the variables in the request that can be used by ${} to ↓ to ↓ to ↓ to ↓ to ↓ to ↓ to ↓ to ↓ to ↓ to ↓ to ↓ to be used by ${} to be used
        Map<String, Object> m = new HashMap<>();
        m.put("xxx", "xxx");
        urlBasedViewResolver.setAttributesMap(m);
    }

    //The parameter converters of springmvc are configured here. Each parameter converter inherits from the PropertyEditorSupport class
    @InitBinder
    public void bind(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateConvertEditor());
        binder.registerCustomEditor(List.class, new StringListConvertEditor());
        binder.registerCustomEditor(int.class, new IntegerConvertEditor());
        binder.registerCustomEditor(String[].class, new StringListConvertEditor());
        binder.registerCustomEditor(boolean.class, new BooleanConvertEditor());
    }

}

The above example also shows how to inject a parameter converter into spring MVC. For example, we need to convert the string "2018-3-9 17:48:00" passed in the foreground to a Date object, and the above DateConvertEditor class can complete the conversion.

In freemarker

If you want to inject variables in freemarker or use shiro tag, we can use FreeMarkerConfigurer object to implement:

@Configuration("Mv6gFBYZ3VDmPeSjTpQC")
public class ShiroTagFreeMarkerConfigurer implements InitializingBean {

    @Autowired
    FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public void afterPropertiesSet() throws IOException, TemplateException {
        //You can use shiro tags in ftl files
        freeMarkerConfigurer.getConfiguration().setSharedVariable("shiro", new ShiroTags());
        //It can be found in the ftl file
        freeMarkerConfigurer.getConfiguration().setSharedVariable("xxx", "xxx");
    }

}

At this point, if we want to use the shiro tag in freemarker, we can do this:

<@shiro.hasPermission name="quanxian">
</@shiro.hasPermission>

Similarly, if you need to use the xxx variable registered above, you can:

${xxx}

Topics: Spring Shiro JSP FreeMarker