0

I need to create a method just like a Controller method, but without mapping it to a request pattern, as it should be called from another piece of code.

The method will take some parameters and, probably, populate the model.

Basically I want Spring MVC to do the job of merging model with the view jsp, and, probably, print it to the HttpServletResponse that I provide and set some other headers like Spring will normally do.

Should be a few lines of code but I can't imagine which:)

1 Answer 1

1

You can try ViewControllerRegistry to generated the view without using the controller.

Please check the example bellow. I am using java based configration.

@Configuration
@EnableWebMvc
public class MVCConfig extends WebMvcConfigurerAdapter {
    @Bean  
    public InternalResourceViewResolver viewResolver() {  
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
        resolver.setPrefix("/WEB-INF/pages/");  
        resolver.setSuffix(".jsp");
        return resolver;  
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("myhome");
        registry.addViewController("/hello").setViewName("helloworld");
        registry.addRedirectViewController("/home", "/hello");
        registry.addStatusController("/detail", HttpStatus.BAD_REQUEST);        
    }    
}

My myhome.jsp file would be

<html>
<head><title>Home Page</title></head>
<body>
   <h3> Home Page </h3>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but it still maps the view to a url; I understand that my caption "without a controller" is misleading, let me change that to "without mapping a controller to a url pattern"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.