PageNotFound - No mapping found for HTTP request with URI - spring

I am getting this error :
o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI
when I try to render a view from an controller/action
I've read about it being that servlet mapping being:
Spring MVC Dispatcher Servlet
/webspring/*
and the star hijacks even the jsp renderings. How can I overcome this ( as simple as possible ) and hopefully without having to resort to .do endings so it becomes controller/action.do
Thanks !
===============EDIT=================
It appears that this happens if your jsp is not in the WEB-INF folder, but outside but in WebContent... any idea why? The jsp's cant be outside? Or shouldnt?
But now I still have problems with linking to a css file... which usually are outside of the web-inf directory, right?
A link:
<link rel="stylesheet" href="webspring/public/main.css" type="text/css"/>
where the directory structure is:
WebContent
WEB-INF
webspring
public
main.css
... is being resolved to
http://localhost:8080/XXXWeb/webspring/auth/webspring/public/main.css
when the style tag is added on page with url:
http://localhost:8080/XXXWeb/webspring/auth/loginForm
auth is the controller.
What is going on here?

I got the same problem with you. So I resolve like this
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/webspring/auth/webspring/public/*</url-pattern>
</servlet-mapping>

Related

Spring #RequestMapping("/favicon.ico") does not work when requested from index.html

I used to get mapping not found for favicon.ico so decided to deal with it.
Easiest thing for me was to add an action to a controller method,#RequestMapping("/favicon.ico").
I no longer get these complaints ( although I don't request favicon.ico myself in an html file, I guess teh browser does this automatically ).
When I visit http://localhost:8080/favicon.ico the action gets hit!
I add the following to my html file:
<link href="/favicon.ico" rel="icon" type="image/x-icon" />
But the action never gets hit.
I've also tried
<link href="http://localhost:8080/favicon.ico" rel="icon" type="image/x-icon" />
but action does not get hit.
I suspect this has to do with get/post request something, and when I request it from the browser manually, a get request is made. When from an html file something else, and Spring won't recognize this.
Please do not recommend me doing:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
as I like to do it from my controller as I have some logic there.
Could there be some cache involved?
EDIT:
I should also mention that i keep getting a tomcat favicon instead. Can't see any info on a favicon.ico ever being requested.
Is tomcat supplying it by default and ignoring to hit/forward it to my action?
I ran into the same issue.
When spring boot starts you can see this in the console:
[...] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
You can disable the spring mvc favicon handler from the application.properties:
spring.mvc.favicon.enabled=false
Source: Spring Boot: Overriding favicon

Spring MVC: how do I configure a web app to call a controller by default?

I'd like my Spring application to call a controller whenever someone opens the application at the root, like this:
http://server.com/myapp
or
http://server.com/myapp/
Ideally, I'd like the user to automatically be taken to /home.do, as in:
http://server.com/myapp/home.do
When I updated web.xml to include /home.do in the <welcome-file-list>, Tomcat returns an error page displaying:
The requested resource (/myapp/) is not available.
When I change <welcome-file-list> to instead take the user to the JSP behind home.do, the page loads, but the JSTL tags in the page cannot access a properties file that is loaded by my Spring configuration.
Any help would be much appreciated!
Create an html page or jsp add it to <welcome-file-list> and add this to the code of your page
<meta http-equiv="refresh" content="0; url=http://server.com/myapp/home.do" />
File list makes the page your default page and redirect will send to your controller without user notice

Spring MVC - generic HTTP handlers

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.

Websphere not using welcome-file-list after mapping *.html to a servlet

I am using IBM WebSphere (WAS) 7.0.0.19 to host a java-based web-app, and I needed to map the extension *.html to a particular servlet so that I could do some server-side scrubbing of user-supplied HTML files. (The server reads the file, augments it with some extra information, and serves up the modified content transparently to the person viewing the page.)
Unfortunately, when I did this, welcome-files stopped working. Previously, if I typed in the URL for a directory, the server would look for index.html and serve up that. Now, I'm just getting a 403 forbidden rule ("Forbidden - by rule."). The access logs don't show anything more--they simply state that directory indexing is forbidden by rule for the server, which is correct. I don't want the webserver to build a table of contents for directories with no index.html, but when there is an index.html, I want it to serve up that file.
My first thought was that it was trying to serve the index.html through my servlet, the servlet was failing to find the file (because the url lacked "index.html"), and therefore it thought there was no index.html. However, I put in some debug code and am quite confident that the servlet code is never getting run when I go simply to the directory itself.
I don't really care whether index.html is served through the servlet or not--in the case of this particular file, the servlet would just spit back the original file anyway. I just want index.html to be served by something.
Here is the relevant section of my web.xml
<servlet-mapping>
<servlet-name>PageScrubber</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
For what it's worth, index.htm and index.jsp were not working before the addition of the servlet mapping. Only index.html worked before. However, now none of them work.
I have used the same web.xml with two Oracle products: WebLogic (WLS) and Oracle Application Server (OAS) with no issues.
I am quite confident that it is just the addition of this scrubber servlet that has caused the problem, because removing that directive caused directory indexing to start working again.
I did find some notes about welcome-file-list not working when using an extended document root, and I tried setting com.ibm.ws.webcontainer.EnablePartialURLtoExtendedDocumentRoot to be true, but that did not seem to change anything.
I'm pretty much out of ideas. Does anyone out there have any thoughts as to why it's not finding my index.html? Thanks in advance!
Caveat: I am working out of memory here.
The Welcome files used to be served by File Serving Servlet (or something that sounds similar to that).
This information would be in the WebSphere extensions file.
I would take a step back and remove your pageScrubber and get the file serving Servlet to serve the welcome files and see that things are working before getting back to using the PageScrubber.
These are my initial thoughts.
HTH

Redirect from index page in Spring and Tomcat

I have a Spring application, which is running on Tomcat say at: http://example.com/foo/
DisplatcherServlet is mapped to app/*, for example, index page is:
http://example.com/foo/app/index.html
This is so because I have other servlets (for HttpRequestHandlers), e.g. mapped to service/*. This scheme is convenient to use because this way app/ and service/ can have different security settings.
What I want is http://example.com/foo to redirect to http://example.com/foo/app/index.html.
How do I achieve this?
In your web.xml, you can define a welcome file, seen when navigating to the root of the app:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
You can then make a tiny index.jsp that redirects to where you want:
<% response.sendRedirect("app/index.html"); %>
You can add the (tuckey) Url Rewrite Filter to your application.
It provides you the functionality to define URL rewrite rules in you Application (ingoing and outgoing).
So you can define a rewrite rule for \ that it rewrites to myApplication.startpage - or something else.
#see: http://mattgivney.blogspot.com/2010/07/how-to-url-rewrite-in-spring-mvc.html - for a short example

Resources