I have tested like default, extension, path(start with '/' and end with '/*') these three are working fine but exact match url pattern '/test' showing 404 error.Please refer this is my exact code in web.xml file for configuring dispatcher servlet.
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
For this url on address bar :http://localhost:8086/MVCFirstApp/test/sugar
I am getting error - 404 page not found for exact match url pattern only.
Please help me out from this, thanks in advance.
Please check few steps before reading the solutions
+ do you have proper controller to handle the requested request(url pattern)
+ do you have proper view returned by the controller
+ and is the base package scanned for controller (annotations)
It would be more convenient if you share the controller code too. But with what I understand with your statement you can try this
add URL patters as
/test/*
Let me explain what this does it will send all the request which comes tru URL /test/ to dispatcher servlet . For example /test/sugar or /test/abc/def/ghi anything, in more simple words any request with URL /test/* is sent to dispatcher servlet which matches and return the proper controller with help of handler mapping .
And make sure you have added the mapping for /test/sugar or /test with proper view in controller . Or if you want sugar to be your value then use #Pathvariable in your controller.
Related
What happens when all of the request mappings in the controller do not match the incoming request? I was using the exceptionhandler catching Exception.class but that did not seem to catch it? Can anyone explain?
According to documentation #ExceptionHandler deal with unexpected exceptions that occur during controller execution. In the case when incoming request do not match any request mapping, controller method even will not be executed.
Spring-MVC based on Servlet technology. All incoming requests will be processed by DispatcherServlet (that registered in web.xml). DispatcherServlet looks for handlers which can process current request. If no appropriate handler will be found (i.e. no appropriate mapped controller's method will be found), DispatcherServlet initiates 404 error.
When an error occurs, the web container(Tomcat or any other servlet container) generates a default page containing exception message. But you can also specify that the container should return a specific error page for a given exception. For process this error manually you should add into web.xml new error-page element, that will allow you map error code to url:
...
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<error-page>
<error-code>404</error-code>
<location>/page-not-found</location>
</error-page>
...
Because location will be processed by Spring-MVC as usual request you can create controller to handle this request:
#Controller
public class ExceptionController{
#RequestMapping("/page-not-found")
public ModelAndView pageNotFound(){
return new ModelAndView("page-not-found");
}
}
Now just create view with name page-not-found.jsp and show exception info as you wish.
How would you make it so that /url works but /url.html or /url.jsp fails? I'd like to setup my pattern to only accept exact matches that have no extension.
Currently in web.xml I use
<servlet-mapping>
<servlet-name>DispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and on my controllers I use
#RequestMapping(value = "/url", method = RequestMethod.GET)
but that allows any extension to be mapped to my controllers so long as 'url' is contained in the requested path. I would like extensions or incorrectly appended characters to fail. Is there a way to do this without parsing the URL string programmatically for each controller?
I am back with working in Springs. I used to work in Springs but blindly, didn't understand much. I used to get a lot of errors, very basic ones, and I am getting them again.
My problem is that, I don't know how the configuration of the Spring-MVC work.
What happens when I run the project from my STS?
I am working on the spring template project in STS.
I am getting this when I run the project.
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/common/] in DispatcherServlet with name 'appServlet'
I am totally fed up and broken.
Just 2 months of break from work, I am back at the starting block.
I don't want to post my code and make the question specific.
I want an answer that explains the way in which the server executes a spring project. Right from the running of an application(basic hello world application) to the display of the home page.
This will be helpful for all the beginners.
I tried searching for such an explanation in the net but I didn't get any proper explanation, but got a lot of basic samples. Those samples are easy to understand but are not explaining the way in which the server goes about.
Note: I am looking for an answer that explains the Springs concept. From the running of an application to the display of a home page. What all happens in this process? Where does the server start with? How does it go about?
Here is the flow initially servlet container loads the web.xml file.In web.xml we will specify that all the requests are handled by the spring FrontController that is DispatcherServlet.
We include it by adding the following code
<servlet>
<servlet-name>dispatcher</servlet-name>
<servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Here it indicate if the url request is of *.htm it is handled by dispatcherServlet then dispatcherServlet load dispatcher-servlet.xml . Where we need to mention the mapping to controller by writing the specific url request such as
<bean name="/insert.htm" class="com.controller.MyController"></bean>
So in bean we mention that for request of /insert.htm it tells the servlet to look in the mentioned class.You need use the Annotation of #RequestMapping above the method for ex
#RequestMapping("/insert.htm")
public ModelAndView insert(HttpServletRequest req,Student student)
{
String name=req.getParameter("name");
int id=Integer.parseInt(req.getParameter("id"));
student.setId(id);
return new ModelAndView("display","Student",student);//It returns a view named display with modelclass name as `Student` and model object student
}
So when a Request url of /insert.htm appears it executes the above method it returns a ModelAndView object nothing but an view.It again goes to dispatcher-servlet.xml and looks for view Resolver the normal code that is to be added is
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
So from this it gets the logical view name and appends the prefix and suffix to it .Finally it displays the content in the view.so it looks for display in view resolver prefixes and suffixes the things and finally returns /WEB-INF/jsp/display.jsp .Which displays the jsp content
You are mapping your Spring servlet only for requests that end with .htm. The request for the root of your application does not end with .htm and so, it does not get picked up by Spring. Edit your web.xml as follows, in order to use Spring for all requests:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then, use this as the controller:
package com.mkyong.common;
#Controller
public class HomeController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
}
The controller intercepts the requests for the context root of the application, adds the msg attribute to the model and redirects to the index view.
So, you need to add the index.jsp file in the /WEB-INF/views/ directory. Inside your jsp, you will be able to use the value of the msg attribute.
From what every you have posted you do no have a request mapping for the url /common/.
You will have to create another request mapping function like the one below in your controller class and create a view file also.
#RequestMapping(value = "/common/", method = RequestMethod.GET)
public ModelAndView common(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView model = new ModelAndView("common");
model.addObject("msg", "hello world");
return model;
}
Fresh off my last adventure, now I'm trying to map more complex URLs and going nuts trying to make it work the way Spring's documentation suggests it should.
Again, the tools are:
Java 1.6
Spring 3.2 MVC
Tomcat 7
What I'm trying to do is match URLs of the form foo/bar/id where id is an integer. The way it seems like I should do it is to annotate my controller method like this:
#RequestMapping("/foo/bar/{id}")
And then have this in web.xml:
<url-filter>/foo/*</url-filter>
Or this:
<url-filter>/foo/bar/*</url-filter>
And then after deploying to Tomcat, I should be able to access /mycontext/foo/bar/id. But that doesn't work.
For completeness, here several variations and results:
Method mapping: /foo, url-filter: /foo, result: /mycontext/foo works.
Method mapping: /foo/*, url-filter: /foo/*, result: successful mapping of method to /foo/* reported at deployment, but accessing /mycontext/foo/bar fails.
Method mapping: /foo/*, url-filter: /foo/bar, result: mapped at deployment, but accessing /mycontext/foo/bar fails.
Method mapping: /foo/bar, url-filter: /foo/bar, result: /mycontext/foo/bar works.
Method mapping: /foo/bar/*, url-filter: /foo/bar/*, result: mapped at deployment, but accessing /mycontext/foo/bar/(anything) fails.
Method mapping: /foo/bar/{id}, url-filter: /foo/bar/*, result: mapped at deployment, but accessing /mycontext/foo/bar/(anything) fails.
All of the failures come with error messages from the DispatcherServlet for mycontext that no mapping was found, even though all of them reported success in setting up mapping at deployment time. Since I'm getting the error from the right DispatcherServlet, that suggests my url-filter settings are fine. But the message about successful mapping at deployment references whatever is in the #RequestMapping annotation, so I don't know what to make of Spring first saying it's fine and then later saying it doesn't match.
Is there something I've failed to understand about wildcards here?
If you have #RequestMapping("/foo/bar/{id}") this is mapped after combining with the DispatcherServlet's url-pattern match. Consider a case for eg, where the url-pattern is as follows:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/dispatcher</url-pattern>
</servlet-mapping>
In this case the DispatcherServlet will map the method only if the call from the client is to : /dispatcher/foo/bar/1
So if you want say #RequestMapping to respond to http://<server>/<context>/foo/bar/1 say, just put your url-pattern for DispatcherServlet as / instead:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Have searched around and have not found a conclusive answer to this.
I have trying to route all http requests through my dispatcher servlet, and then onto a specific controller. Ultimately I want to be able to handle resource, AJAX and a.n.other request through the central point.
I currently have the url mapping /* in place to do this. My controllers use #RequestMapping("/[My resource].*") to capture my .htm requests. Unfortunately Spring appears to use RequestDispactcher.forward to resolve the .jsp from the InternalResourceViewResolver which is then hitting the front controller again and ultimately causing a 404 error.
My question is, am I able to setup a generic catch all that will handle any HTTP request other than the regular view request ?
The HTTP handler must be able to pass requests on to other servers and resolve internal and external resources e.g. images, css etc.
Regards,
Andy
Regards
A think a better idea is to change the servlet-mapping of DispatcherServlet to / instead of /*, this is because /* makes all request come to this servlet, instead like you have found for the jsp forwards also, inspite of the fact that there is a JSPServlet mapping for the jsps, the / mapping on the other hand will be defaulted to only if a specific mapping is not found for the requested path.
Keep the app servlet mapping to / in web.xml. Like shown below.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To resolve other resources add following tag in your dispatcher servlet xml.
Here resources is the folder containing js, css, images. It is stored under Webcontent folder in maven web application structure. Change it according to your project structure.
<resources mapping="/resources/**" location="/resources/" />
Try this.