1. Using Explicit Views and their View Names:
- In Spring MVC, you can use explicit views by returning a
View
object from your controller method. - Example:
@GetMapping("/example") public View handleRequest() { return new ModelAndView("exampleView"); }
2. Using the Redirect View:
- The RedirectView class allows you to redirect to another URL.
- Example:
@GetMapping("/redirectExample") public RedirectView handleRedirect() { return new RedirectView("/newLocation"); }
3. Configuring View Resolution:
- Configure view resolution by defining ViewResolver beans in your Spring configuration.
- Example (Java Config):
@Bean public ViewResolver internalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; }
4. Creating a JSP View:
- Use JSP views for rendering dynamic content.
- Example:
@GetMapping("/jspExample") public String handleJsp(Model model) { model.addAttribute("message", "Hello, JSP!"); return "exampleJspView"; }
5. Using Implicit Views with Model Attributes:
- Spring allows you to return a logical view name, and it will look for a matching view.
- Example:
@GetMapping("/implicitExample") public String handleImplicit(Model model) { model.addAttribute("message", "Implicit View Example"); return "implicitExampleView"; }
6. Configuring View Name Translation:
- Configure view name translation using
LocaleResolver
for internationalization. - Example:
@Bean public LocaleResolver localeResolver() { return new AcceptHeaderLocaleResolver(); }
7. Using @ModelAttribute:
@ModelAttribute
is used to bind method parameters or method-level attributes to the model.- Example:
@ModelAttribute("commonAttribute") public String addCommonAttribute() { return "This is a common attribute"; }
8. Returning Response Entities:
- Use ResponseEntity to have full control over the HTTP response.
- Example:
@GetMapping("/responseEntityExample") public ResponseEntity<String> handleResponseEntity() { return ResponseEntity.ok("Response Entity Example"); }
9. Configuring Message Converters:
- Configure message converters to convert between HTTP requests and Java objects.
- Example:
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MappingJackson2HttpMessageConverter()); } }
10. Configuring Content Negotiation:
- Content Negotiation allows your application to respond to different types of requests.
- Example:
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false) .favorParameter(true) .parameterName("format") .ignoreAcceptHeader(true) .useJaf(false) .defaultContentType(MediaType.APPLICATION_JSON); } }
11. Using @ResponseBody:
@ResponseBody
is used to indicate that the return value of a method should be directly written to the response body.- Example:
@GetMapping("/responseBodyExample") @ResponseBody public String handleResponseBody() { return "Response Body Example"; }
In the context of Jakarta EE 8, integrating these Spring MVC features allows you to manage views, handle responses, and configure various aspects of content negotiation within your web application.