No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp] [duplicate]
Asked Answered
R

16

93

My handler forwards to internalresourceview 'apiForm' but then i get error 404 RequestURI=/WEB-INF/pages/apiForm.jsp. I'm sure apiForm.jsp located in /WEB-INF/pages/

13:45:02,034 DEBUG [org.springframework.web.servlet.view.JstlView] - Forwarding to resource [/WEB-INF/pages/apiForm.jsp] in InternalResourceView 'apiForm'
13:45:02,035 DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'testapp2' determining Last-Modified value for [/WEB-INF/pages/apiForm.jsp]
13:45:02,038 DEBUG [org.springframework.web.servlet.DispatcherServlet] - No handler found in getLastModified
13:45:02,038 DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'testapp2' processing request for [/WEB-INF/pages/apiForm.jsp]
13:45:02,038 WARN [org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp] in DispatcherServlet with name 'testapp2'
13:45:02,045 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request
13:45:02,048 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request

this is how my dispatcher.xml look like..

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>
Replacement answered 12/8, 2009 at 13:54 Comment(0)
C
155

Looks like DispatcherServlet is trying to process the request for apiForm.jsp, which suggests to me that your web.xml servlet-mapping is directing requests for that space to DispatcherServlet.

You might have something like this?

<servlet-mapping>
  <servlet>dispatcher</servlet>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit

 <servlet-mapping>
  <servlet>dispatcher</servlet>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>
Caras answered 12/8, 2009 at 14:54 Comment(6)
yes, u are right. can u explain more, why i cannot set dispatcher as /* ?Replacement
When you set the url-pattern to /* then all requests will be sent to that DispatcherServlet, which includes the request for JSP rendering. Though it's not true, it's sometimes useful to think of the InternalResourceView (and derived like JstlView) as another HTTP request, since that way you'll see why a request for the JSP is getting picked up by the DispatcherServlet.Caras
when i set to / , i the request is render fine. only when i set /* . what is the different / and /* ?Replacement
<url-pattern>/</url-pattern> only matches the URL host/servlet <url-pattern>/*</url-pattern> matches everything under host/servlet, such as /index.html, /foo.jpg and, most importantly in this case, /WEB-INF/pages/apiForm.jsp the * is the wildcard, which says "anything" In the earlier suggestion, *.do matches anything that ends in .do, for example, /foo.do, /foo/bar.do. It doesn't match anything ending in jsp, so a request for /WEB-INF/pages/apiFrom.jsp is not matched, and is not routed to the DispatcherServletCaras
Worked for me too. Thanks goes to @ptomli, to add to his comment if you are creating a RESTful api server where you have a collection of web services with no presentation layer then <url-pattern>/*</url-pattern> is useful since there is no html/jsp files in your web application. In a normal scenario where you have all the MVC layers you gotta consider a proper url pattern to match your view layer jsp/html files. <url-pattern>*.do</url-pattern> represents the business/controller layer actions.Fascicule
Why did you suggest *.do mapping? He didn't said anything related to .do at the end of the url.Digress
T
90

Yes, I know I'm late to this party but it might help others.

The servlet container chooses the mapping based on the longest path that matches. So you can put this mapping in for your JSPs and it will be chosen over the /* mapping.

<servlet-mapping>
  <servlet-name>jsp</servlet-name>
  <url-pattern>/WEB-INF/pages/*</url-pattern>
 </servlet-mapping>

Actually for Tomcat that's all you'll need since jsp is a servlet that exists out of the box. For other containers you either need to find out the name of the JSP servlet or add a servlet definition like:

<servlet>
  <servlet-name>jsp</servlet-name>
  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
Trivia answered 27/5, 2010 at 17:49 Comment(6)
This is perfect, allows for no extensions! Thanks,Apocrypha
Jetty also uses the Jasper servlet by default under the servlet name "jsp", so the same mapping would work with Jetty.Sprain
I can't get it to work. It seems that the jsp servlet doesn't exist in Tomcat 7, and default servlet threats jsp as static pages.Varicotomy
(sorry, treats) Finally it works (with Jasper). The words " for Tomcat that's all you'll need" were misleading, thank you anyway!Varicotomy
does that not work for Tomcat anymore?Trivia
This is the cleanest solution I have managed to find. It's the best alternative for the lack of url-mapping exclusion support in web.xml. My dispatcher servlet now easily maps to '/' and '/*' as intended. Thank you!Anjelicaanjou
S
19

Just add <mvc:default-servlet-handler /> to your DispatcherServlet configuration and you are done!

Sclerodermatous answered 21/9, 2014 at 6:35 Comment(3)
The OP really ought to change the accepted answer. Great tip.Filiano
Great answer! It's really weird that it worked without this declaration before. I upgraded Eclipse from Juno SR1 to Luna SR2 and bam, it stopped working. Without this answer, I wouldn't have gone far. Thank you!Vincent
what about static css/js files? they are still failing to load event after adding <mvc:default-servlet-handler />Leialeibman
M
13

you will get a No mapping found for HTTP request with URI error

if you scanned the wrong package

e.g your controller is in my.package.abc but you mistakenly put

<context:component-scan base-package="my.package.efg*" />

or

@ComponentScan("my.package.efg*")

which in the sense, your controller doesn't get scanned into the web application context, when request comes in not just url, but the entire class is not found!

Mingrelian answered 30/5, 2016 at 1:25 Comment(1)
This isn't an answer to the question asked here, which is about the error being generated after redirecting a response to a JSP view. If this were the problem, then the redirect wouldn't be happening, and the JSP file wouldn't be referenced in the error message.Gunrunning
A
9

I think I read the entire internet to figure out how to get sitemesh to handle my html paths without extension + API paths without extension. I was wrapped up in a straight jacket figuring this out, every turn seemed to break something else. Then I finally came upon this post.

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/WEB-INF/views/*</url-pattern>
 </servlet-mapping>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/WEB-INF/decorators/*</url-pattern>
</servlet-mapping>

Enter this in your dispatcher-servlet.xml

<mvc:default-servlet-handler/>
Athletics answered 10/6, 2013 at 21:46 Comment(0)
I
9

Solution that helped me is: do not map DispatcherServlet to /*, map it to /. Final config is then:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        ...
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Inenarrable answered 23/5, 2014 at 23:50 Comment(0)
A
8

With Spring 3.1 and Tomcat 7 I got next error:

org.springframework.web.servlet.DispatcherServlet noHandlerFound No mapping found for HTTP request with URI [/baremvc/] in DispatcherServlet with name 'appServlet'

And I needed to add to web.xml next configuration:

<welcome-file-list>
    <welcome-file/>
</welcome-file-list>

And the application worked!

Authorization answered 12/4, 2012 at 8:7 Comment(1)
This isn't an answer to the question asked here, which is about the error being generated after redirecting a response to a JSP view. If this were the problem, then the redirect wouldn't be happening, and the JSP file wouldn't be referenced in the error message.Gunrunning
K
8

Simple check can be made. I am currently using Spring MVC architecture along with hibernate. I had missed writing @Controller annotations just above class name. This was causing the problem for me.

@Controller
public class MyClass{
    ...
}

Hope this simple check solves your problem.

Kitty answered 29/11, 2012 at 6:55 Comment(2)
This worked for me ONLY ...AFTER ... I had to add the following line: @RequestMapping(value = "/", method = RequestMethod.GET)Assegai
This isn't an answer to the question asked here, which is about the error being generated after redirecting a response to a JSP view. If this were the problem, then the redirect wouldn't be happening, and the JSP file wouldn't be referenced in the error message.Gunrunning
D
3

I had the same problem, Of course there was a little difference. The story was that when I was removing the below line:

<mvc:resources mapping="/resources/**" location="classpath:/resources/" />

Everything was OK but in presence of that line the same error raise.

After some trial and error I found I have to add the below line to my spring application context file:

<mvc:annotation-driven />

Hope it helps!

Disjointed answered 25/5, 2016 at 3:46 Comment(0)
R
2

This could also happen when your app doesn't actually compile, yet it's still launched in Tomcat. When I saw this happen, it wasn't compiling because the project had a "project specific" JDK specified, and the code was checked out on a machine that didn't have that specific JDK. Eclipse defaulted to a JRE instead, not a JDK, and then the app wasn't compiled.

To fix it in our specific case, we just turned off "Project Specific Settings" here:

"Project | Properties | Java Compiler"

Here's more detailed info on how to do this: https://mcmap.net/q/225553/-how-do-i-get-eclipse-to-use-a-different-compiler-version-for-java

Richella answered 16/3, 2012 at 12:44 Comment(1)
sir u are a life saver.Midway
M
2

Same answer as Brad Parks... more text though

I had the exact same problem and tried the above solutions along with many others, all with negative results. I even started out with a new, fresh Dev env and simply installed a spring-mvc-template and tried to run it directly after install (should work, but failed for me)

For me the problem was that I was using jdk1.6 in my project, but my selected execution environment in eclipse was jdk1.7. The solution was to change the project specific execution environment settings so that this project is set to jdk1.6. Right click project --> Properties --> Java Compiler --> Check "Enable project specific settings" if it's not already checked --> select the appropriate jdk (or add if it's not installed).

I hope this can help someone and save that persons time, because I have spent the last few days looking for the answer on every corner of the Internet. I accidently stumbled upon it myself when I started to get desperate and look for the solution in areas where it (according to my brain) was less likely to be found. =)

My 2 cents. Thanks!

Edit1: Use project specific settings

Edit2: Just realized Brad Parks already answered this in this very thread. Well, at least I got the "Editor"-badge out of this one =D

Macedonian answered 17/3, 2013 at 17:15 Comment(1)
still relevant 7 years after.cheers man u just saved my dayMidway
M
1

Unfortunately, this is a rather broad class error message. Yet another thing which could be going wrong is if you are missing some classes/jars. For example, if you are missing the spring-expression jar file, the dispatch-servlet is not going to be able to locate your controller no matter how hard you try and how correct everything else is configured.

Masterful answered 31/5, 2013 at 20:45 Comment(1)
The information in the question quite clearly shows that the controller was being located, because it includes a log showing the controller method executing, and then sending a redirect to a JSP view, therefore this is not a solution to this question.Gunrunning
C
1

I have encountered this problem in Eclipse Luna EE. My solution was simply restart eclipse and it magically started loading servlet properly.

Carthusian answered 2/7, 2015 at 10:31 Comment(1)
Better to clean the project and run this is the way to make application not required for restart eclipse.Paradise
P
0

What you need is to have a controller that responds to the url first which then renders your jsp. See this link for a solution.

Preview answered 12/8, 2009 at 15:2 Comment(0)
L
0

"/openStudentPage" is the page that i want to open first, i did :

 @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
     return "redirect:/openStudentPage";
    }

@RequestMapping(value = "/openStudentPage", method = RequestMethod.GET)
public String listStudents(Model model) {
    model.addAttribute("student", new Student());
    model.addAttribute("listStudents", this.StudentService.listStudents());
    return "index";
}
Limoges answered 22/1, 2015 at 10:46 Comment(0)
V
-4

change the your servlet name dispatcher to any other name .because dispatcher is predefined name for spring3,spring4 versions.

<servlet>
    <servlet-name>ahok</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ashok</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Vomitory answered 19/7, 2014 at 12:40 Comment(1)
org.springframework.web.servlet.DispatcherServlet is pure controller so no need to change some another controller.Paradise

© 2022 - 2024 — McMap. All rights reserved.