Having issue with mapping in spring dispatcher servlet - spring

I have a JSP page that contains a form and on submitting that form I need to call GET method. But with my current mapping, when I try to load my JSP page then instead of loading the JSP page it calls GET method.
Following are the mappings that I made.
web.xml:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
TestHarnessController :
#Controller
#RequestMapping("/testHarness")
public class TestHarnessController {
#RequestMapping(method = RequestMethod.GET)
public String handleGetRequest(HttpServletRequest request, HttpServletResponse response) {
return null;
}
#RequestMapping(method = RequestMethod.POST)
public String handlePostRequest(HttpServletRequest request, HttpServletResponse response) {
return null;
}
}
And my JSP page url is:
http://localhost.ms.com:8080/DataBox/test/testHarness.jsp
Please give me some suggestions as I am struggling with this since 2 -3 days.

Related

How to configure Spring errror page in JSON format with web.xml and an error controller

With Spring 5, I am currently able to configure an error page in src/main/webapp/web.xml, i.e. the following configuration is added:
<error-page>
<location>/WEB-INF/error.html</location>
</error-page>
In this way, the error.html will be rendered when there is Exception in the controller. However, this error.html is in html format other than the expected JSON format.
I tried to make an error controller with some code like this:
#RestController
#RequestMapping(value = "/handler")
public class ErrorController {
#RequestMapping(value = "/errors")
public String renderErrorPage(HttpServletRequest httpRequest) {
System.out.println("DEBUG::come to error page");
return "test error";
}
}
In the same time configured error-page as such:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<error-page>
<location>/handler/errors</location>
</error-page>
But the ErrorController cannot be invoked.
Question: How to configure Spring error page in JSON format with web.xml and an error controller?
I eventually realized that the servlats are filtered with /rest/* through the servlet-mapping tag, so the location of the error-page has to be prefixed with /rest, meaning the error-page tag should be configured as such:
<error-page>
<location>/rest/errors</location>
</error-page>
Correspondingly the controller can be configured like:
#RestController
//#RequestMapping(value = "/handler")
public class ErrorController {
#RequestMapping(value = "/errors")
public String renderErrorPage(HttpServletRequest httpRequest) {
System.out.println("DEBUG::come to error page");
return "test error";
}
}

how to add multiple mapping to web.xml

i want to communicate 2 application , in my first application i'm using a restTemplate to send a notification to the second app , that's why i need to have a Rest endpoint inside my second App .
in the First App ( the one sending notification ) this is the method i use to send notification:
public void setSomething() {
String operation = "I'm sending you the operation ID";
// URL to the SelectSystem App
System.out.println("Tryin to send something to selectsystem-view");
final String uri = "http://localhost:8080/from";
RestTemplate restTemplate = new RestTemplate() ;
if (operation != null) {
restTemplate.postForObject( uri,operation, String.class);
System.out.println("Send is done !!");
}
}
In my second App (the one receiving)this is the class receiving the notification :
#RestController
public class NotificationReceiver {
#RequestMapping(value = "/from", method = RequestMethod.POST)
public ResponseEntity<String> createEmployee(#RequestBody String greeting) {
if (greeting !=null) {
System.out.println("The result From the other App is :"+greeting);
}
return new ResponseEntity(HttpStatus.CREATED);
}
#RequestMapping(value = "/from",method = RequestMethod.GET)
public void greeting() {
System.out.println("testing the restController");
}
}
the problem i'm having is that i can't map my RestController from the web.xml since i already have a JSF mapping , this is the jsf mapping web.xml :
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>modules/index.xhtml</welcome-file>
</welcome-file-list>
Any idea how to Map my RestController?

How to globally handle 404 exception by returning a customized error page in Spring MVC?

I need to return a customized error page for HTTP 404, but the code does not work. I already read the stackflow 1,2, 3 and different online + articles but my case is quite different with those or at least I can not figure out the issue.
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
For example, it is suggested to use an action name in web.xml to handle the exception, it might work but I think is not a good method of doing it.
I used following combinations:
1)
#Controller //for class
#ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
2)
#Controller //for class
#ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
#ExceptionHandler(ResourceNotFoundException.class) //for method
3)
#ControllerAdvice //for class
#ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
3)
#ControllerAdvice //for class
#ResponseStatus(HttpStatus.NOT_FOUND) //for method
4)
#ControllerAdvice //for class
#ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
#ExceptionHandler(ResourceNotFoundException.class) //for method
Code
#ControllerAdvice
public class GlobalExceptionHandler {
#ResponseStatus(value = HttpStatus.NOT_FOUND)
public String handleBadRequest(Exception exception) {
return "error404";
}
}
DispatcherServlet does not throw an exception by default, if it does not find the handler to process the request. So you need to explicitly activate it as below:
In web.xml:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
If you are using annotation based configuration:
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
In the controller advice class:
#ExceptionHandler
#ResponseStatus(HttpStatus.NOT_FOUND)
public String handleExceptiond(NoHandlerFoundException ex) {
return "errorPage";
}
You can use the #ResponseStatus annotation at the class level. At least it works for me.
#Controller
#ResponseStatus(value=HttpStatus.NOT_FOUND)
public class GlobalExceptionHandler {
....
}
You can add the following in your web.xml to display an error page on 404. It will display 404.jsp page in views folder inside WEB-INF folder.
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/404.jsp</location>
</error-page>

Not being able to find my mapping anymore (spring - controller)

I am not being able to find my mapping anymore (I made it work. But then, I made changes, but probably due to cache, I didn't realize when it stopped working :-( ). It gives 404 error now when I click on submit on the form. Any help would be appreciated. Thank you in advance!
web.xml:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
JSP:
<form:form method="POST" action="/app/app/studentSearchById" commandName="student">
Controller:
#Controller
// #RequestMapping("/studentSearch")
public class StudentSearchController {
// #Autowired
//StudentService userService = new StudentService();
// #RequestMapping(value="/StudentSearchById", method = RequestMethod.GET)
// public ModelAndView searchById(#PathVariable Long studentId) {
#RequestMapping(value = "/studentSearchById", method = RequestMethod.POST)
public ModelAndView searchById(#ModelAttribute("student") Student student, Map<String, Object> map,
HttpServletRequest request) {
StudentService userService = new StudentService();
Student studentFound = userService.findStudent(student.getStudentId());
ModelAndView modelAndView = new ModelAndView();
// modelAndView.addObject("students", studentFound);
return modelAndView;
}
Thank you a lot in advance!
When you are using stereotype annotation (#Controller , #Service, #Repository) then its better to use context: component-scan tag to say that Spring has to scan packages searching the annotation and register beans within the application context.
I spent hours on this... just after posting, I figured out...
I had changed the line on my spring-servlet.xml. Putting the line as before, it worked:
<context:component-scan base-package="my.controller.package" />

.load() jquery with Servlet

Is it possible to load the contents in a div through a servlet via .load() jquery ?
I tried this
$('#getDetails').load('getDetails');
But its not working !! What can be an alternative to this ?
Thanks
Did you read the documentation? If there's one parameter, it must be a URL. getDetails alone apparently isn't.
Your servlet has to override method doGet, like this:
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// implementation
}
In the web.xml, you put servlet mapping, like this:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.myservlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</servlet>
Then, you call servlet from JQuery context, like this:
function someFunction() {
$("#yourDivId").load("./servlet");
}
Cheers!!
got it.
I changed the method of servlet to do get, and passed a parameter with servlet name.
$('#getDetails').load('getDetails?process=u');
Its working now.
Thanks ..

Resources