How to include JSP in Spring MVC contoller rather than forward? - java

In my Spring MVC controller, I have a single method process():
#Controller
#RequestMapping("/Foo/dash")
public class MyController {
#RequestMapping( params = "action=loadDashboard")
public String process() {
return "/Moo/dash/bar";
}
}
It is my understanding that in this case, upon hitting the return statement, Spring MVC will perform a forward to the specified JSP file.
How can I change the return statement, and/or the method, so that instead of a forward, Spring MVC will perform an include of the specified JSP?

Related

Spring Boot with HTML

I hava Spring Boot Application.I want to show html.
index.html location is following
templete/view/index.html
Controller.java
#Controller
public class Controller {
#RequestMapping(value = "/view", method = RequestMethod.GET)
public String index() {
return "/view/index.html";
}
}
return "view/index";
Should be what you need. In the future you might consider saying what you expect to happen and what actually happens, even better providing an error message which might give a hint like your file could not be found.

How to get URI value coming from HTML to Variable in Spring (There are multiple Links associated with corresponding method)

I have multiple links in my HTML, which are referring to URI in Controller Class,
How can I get this URI in some variable which can be used further, at last, I want to store these URI in DB.
HTML Code :
<td>Win Report</td>
<td>Win Report</td>
Spring Controller Class :
#RequestMapping(value = "/ui/report/win", method = RequestMethod.GET)
public String winReport() {
return "win_report";
}
#RequestMapping(value = "/ui/report/niw", method = RequestMethod.GET)
public String niwReport() {
return "niw_report";
}
You can use the below solution to retrieve the page Url as well as avoid using repetitive method calls.
You can use a Spring Boot HandlerInterceptor, here's a brief description of the same :
Handler interceptors are used when you want to apply specific
functionality to certain or all requests.
Handler Interceptors should implement the interface HandlerInterceptor. HandlerInterceptor can be used to avoid repetitive handler code.
We can use HandlerInterceptor for different purposes like authorization checks, locale checks, logging, creating common application parameters etc.
HandlerInterceptor works similar to the servlet filter. But in some cases filters are more powerful than HandlerInterceptor.
In Spring-MVC the HandlerInterceptor is configured in spring application context xml file or by Java configuration.
HandlerInterceptor has three methods.
preHandle( ) : It is executed before actual handler is executed.
postHandle( ) : It is executed after handler is executed.
afterCompletion( ) : It is executed after the complete request is finished.
For more details, you can use an example from the below link
https://www.tuturself.com/posts/view?menuId=3&postId=1071
You can get the entire sample project which can help you with the setup at
https://github.com/ninja-panda
To get the request url you can do the following:
#RequestMapping(value = "/ui/report/win", method = RequestMethod.GET)
public String winReport(HttpServletRequest request){
String request = request.getRequestURI();
// do somehting here
return "win_report"
}
Spring will automatically inject the HttpServletRequest.
Update:
If your want get the urls for all of your methods in your controller, you can go with RequestMappingHandlerMapping:
private final RequestMappingHandlerMapping handlerMapping;
#Autowired
public YourController(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
With handlerMapping.getHandlerMethods(), you can access all mappings decleared in your controller. With reflection and getMappingAnnotation, you can then read the value of each RequestMapping annotation.
You can try the getServletPath() like following:
#RequestMapping(value = "/ui/report/win", method = RequestMethod.GET)
public String winReport(HttpServletRequest request){
String mapping = request.getServletPath();
// do somehting here
System.out.println(mapping); // Will print /ui/report/win
return "win_report"
}

HTTP 404 Not Found Error in Spring 3.2.8 application

I have an application based in the Spring Web model-view-controller (MVC) framework
I have this controller
#Controller
public class ApplicantApplicationsListController extends ApplicantController {
/**
* #throws Exception
*
*/
#RequestMapping(value = { "/medrano/applicant/home",
"/medrano/applicant/home/"}, method = {RequestMethod.GET})
public String viewProductApplications (#ModelAttribute("applicationApplicationsListForm") final ApplicationApplicationsListForm applicationApplicationsListForm,
HttpServletRequest request,
Model model ) throws Exception {
return "applicantApplicationsView";
}
But I got a 404 in the browser when I put
http://127.0.0.1:7001/cage/medrano/applicant/home
You have a mapping problem with your request mapping:
The annotation #RequestMapping value property expects an array of Strings, in your case:
value = {"/medrano/applicant/home",
"/medrano/applicant/home/",}
Is not a valid String[], you have an additional , at the end, just remove it.
You can check the Spring MVC #RequestMapping Annotation Example with Controller, Methods, Headers, Params, #RequestParam, #PathVariable tutorial for further exmaples on how to use it.
Edit:
There's no need to use the brackets with a single value for both
value and method properties.
And why would you use the same value "/medrano/applicant/home"
twice in your RequestMapping.
It could simply be like this:
#RequestMapping(value = "/medrano/applicant/home",
method = RequestMethod.GET)
I forgot to Configure the Spring DispatcherServlet in the file web.xml with:
...
<servlet-mapping>
<url-pattern>//medrano/applicant/home</url-pattern>
<url-pattern>//medrano/applicant/home/</url-pattern>
</servlet-mapping>
...

Spring MVC - What is responsible for routing the controller response to the template?

I started developing a Spring MVC + Thymeleaf (for the V) recently and I'm trying to understand how it works.
Let's say I have a controller method like this:
#RequestMapping(value = "/")
public String index() {
return "home";
}
And a template called home.html.
What is reponsible for routing the controller to the respective view? Is it thymeleaf? Or the Spring framework?
Thank you
Spring's ViewResolver(s) do this:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/ViewResolver.html
Which view resolver(s) are active and where they look for templates is customizable based on your individual Spring configuration.

Spring Mvc Web Application with Rest interface

in my exercise i have to develop a spring application which should be accessible through a WebGUI AND a REST service.
Now i browed through the examples of Spring MVC, there is this hello world tutorial on Spring MVC.
The controller looks like as follows:
#Controller
#RequestMapping("/welcome")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
Then i looked through the Spring REST example which looks like this:
#Controller
#RequestMapping("/movie")
public class MovieController {
#RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getMovie(#PathVariable String name, ModelMap model) {
model.addAttribute("movie", name);
return "list";
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public String getDefaultMovie(ModelMap model) {
model.addAttribute("movie", "this is default movie");
return "list";
}
}
Now I am wondering, how do these two examples (Spring-mvc and Spring-rest) differ?
They both use the same annotations and work similar. Aren't that both just REST examples?
How can I provide a Rest-Interface to a Spring-MVC application?
regards
In order to provide rest interface to Spring MVC application, you can apply #RequestMapping annotation with a path name to each of the methods in controller, this creates a unique URL path for each of the rest services you would like to provide.
Meaning, the rest services are nothing but the methods in Spring MVC controller with #RequestMapping annotation.
If you would like to learn how Spring MVC supports Rest Based services, the below link might help:
http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/#features
Both samples are about Spring Web MVC.
You should pay more attention to definitions, like what is REST
https://en.wikipedia.org/wiki/Representational_state_transfer
Representational State Transfer is intended to evoke an image of how a
well-designed Web application behaves: presented with a network of Web
pages (a virtual state-machine), the user progresses through an
application by selecting links (state transitions), resulting in the
next page (representing the next state of the application) being
transferred to the user and rendered for their use.
Spring Web MVC greatly facilitates developing REST web APIs and that's it.
Rememeber #ResponseBody as return type on method is going to be REST.
ofcourse returned object can be negotiated with either JSON or XML.

Categories

Resources