Accessing static html file within Spring MVC - spring-mvc

I have set up mvc:resources in my mvc--dispatcher-servlet.xml as below
<mvc:resources mapping="/resources/**" location="resources/" />
<mvc:resources mapping="/favicon.ico" location="resources/images/favicon.ico" />
<mvc:resources mapping="/maintenance" location="resources/html/maintenance.html" />
So far it only works for the first two location (resource mappings). I can only access the static html by supplying the full URI
http://mydomain/resources/html/maintenance.html
instead of
http://mydomain/maintenance
UPDATED
I added <http pattern="/maintenance" security="none" /> in my spring-security-context.xml and it gives me new error, HTTP Status 404 - The requested resource is not available.
What did I miss here ?

I just found out the issue is caused by this
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The easy way out for me was to change the maintenance URI to /maintenance.html, thus the request is not 'handled' by the servlet.
If there's anyone can point out a better solution which allows me to access it as /maintenance, I'll accept that as the answer.

Related

Tomcat + Spring MVC: CSS and Javascript are not loaded in SSL setting

I am using Tomcat (6.0.26) and Spring MVC for a website on Windows 7. I would like to test whether it works on SSL for certain pages or directories.
I have a self-assigned SSL certificate (file: .keystore) in C:\Users\Me with the default password "changeit".
In my Spring's security context, I have the following:
<http auto-config="false" use-expressions="true" request-matcher="regex" >
....
<intercept-url pattern="^\/login$" requires-channel="https" />
<port-mappings>
<port-mapping http="8080" https="8443"/>
</port-mappings>
</http>
When I click http://localhost:8080/login, I am redirected to https://localhost:8443/login, which works as I hoped for. However, only SOME, not all, css files get loaded and NONE of my javascript files get loaded. All my css and javascript files are local.
Here is how I have for css files in JSP:
<link rel="stylesheet" href="/css/styles.css" />
I have this in Spring context for CSS and JS files
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
Here is how I have for javascript files and what is shown in Firebug:
<script src="/js/my.js"></script>
this leads to Status being "302 Moved Temporarily". no file content is loaded
<script src="//js/my.js"></script>
this leads to Status being "Aborted"
What went wrong? How to fix it? Thanks!
(1) Add this to your web.xml
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
(2) add <mvc:annotation-driven /> to your context
(3) try path as /yourApp/css/*.css

Spring mvc 3.2 and extjs 4.1.3 : do I have to define mvc:resources mapping for al requestMapping path?

I have a problem using spring mvc 3 and extjs 4 or more precisely a problem of path when exjs try to load my controllers or my images.
The structure of my webapp is like this :
src
-- webapp
-- app
-- extjs4.1.3
-- css
-- icons
-- WEB-INF
-- jsp
app.js
I have a controller annoted like this
#RequestMapping(value = "/path/init.do", method = RequestMethod.GET)
It return a view name that match a jsp in my WEB-INF/jsp folder. This jsp load extjs files.
ViewResolver :
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>tiana Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Because my url pattern is "/", I had to make use of the mvc:resources magic tag available since spring 3.0.5 (approximatively) like this :
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/app/**" location="/app/" />
<mvc:resources mapping="/extjs-4.1.3/**" location="/extjs-4.1.3/" />
<mvc:resources mapping="/app.js" location="/app.js" />
I know it could be better if I place all those things in a resources folder. I would have to write only :
<mvc:resources mapping="/resources/**" location="/resources/" />
But I don't think it'll solve my problem
My jsp file is like this :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>My jsp title</title>
<link rel="stylesheet" type="text/css" href='<c:url value="/extjs-4.1.3/resources/css/ext-all.css" />'>
<link rel="stylesheet" type="text/css" href='<c:url value="/css/myStyle.css" />'>
<script type="text/javascript" src='<c:url value="/extjs-4.1.3/ext-all-debug.js" />'></script>
<script type="text/javascript" src="<c:url value="/app.js" />"></script>
</head>
<body></body>
</html>
First of all I had to write the references to my resources files using jstl tags.
Without those tags, if I keep url like /myresource.xxx, the url will be
localhost:8080/myresource.xxx
And if I write myresource.xxx without the slash, it is
localhost:8080/mywebapp/path/myresource.xxx
But it works like this, any suggestion would be appreciated to skip the jstl dependency but it works.
My real problem is that my app.js will load extjs's controllers and for that it uses some kind of 'window.location' to construct the url. In my case, it will be
localhost:8080/mywebapp/path/app/controller/myController.js
but i'd like it to be
localhost:8080/mywebapp/app/controller/myController.js
I face the same kind of problem if I put an icon in my extjs files, the url will be
localhost:8080/mywebapp/path/icons/myIcon.png
and not
localhost:8080/mywebapp/icons/myIcon.png as expected.
I can solve this by adding
<mvc:resources mapping="/path/app/**" location="/app" />
<mvc:resources mapping="/path/icons/**" location="/icons" />
But I can believe that each time I define a new requestMapping in a Spring mvc controller, "path2" for example, I'll have to define a corresponding mvc:resources mapping.
I tried to define a global "baseUrl" javascript variable in my jsp file, prefixing the appFolder by this variable in the app.js file and prefixing all icons and things used in extjs code by this variable but it's not either acceptable.
What have I missed ? I googled a lot and I came to this "/" url-pattern and mvc:resources method (which seems very clean for spring mvc webapp) but I cannot solve my "uri" problem (I don't know how the path generated by the requestMapping is called in real english).
Thank you so much if you can help me and if not, thank you for your time
You can try to put all of your resources (i'm not sure which of your contents is a static resource) in:
-- webapp
--resources
-- app
-- extjs4.1.3
-- css
-- icons
-- WEB-INF
-- jsp
app.js
then you define a single mapping:
<mvc:resources mapping="/resources/**" location="/resources/" />
at this point you'll access the css folder for example using:
<c:url value="/resources/css/myStyle.css" />
and any other of your paths in the same way or you can try by using (but not tested):
<mvc:resources mapping="/**" location="/resources/" />
and i suppose you'll access your css folder:
<c:url value="/css/myStyle.css" />

Spring MVC 3.1.1 and JBoss 7.1 can't get the static content to work

I am using Eclipse with JBoss support installed, and have setup JBoss 7.1 as my server of choice.
In my Spring MVC Application i have the following structure
main/webapp/resources/static/{css,img,js}
main/webapp/WEB-INF/classes/messages_XX.properties
main/webapp/WEB-INF/jsp/<name>.jsp
my dispatcher-context.xml contains the following
<mvc:annotation-driven />
<context:component-scan base-package="com.springmvc.test" />
<mvc:resources mapping="/resources/**" location="/resources" />
And it doesn't work
i have tried reaching the content using different paths:
<ip>:8080/test/resources/static/img/logo.png
<ip>:8080/resources/static/img/logo.png
<ip>:8080/test/static/img/logo.png
but it always returns 404, e.g.
HTTP Status 404 - /resources/static/img/logo.png
type Status report
message /resources/static/img/logo.png
description The requested resource (/resources/static/img/logo.png) is not available.
JBoss Web/7.0.13.Final
i already looked around and have tried different options mentioned by different people, but none of it works.
what am i doing wrong here?
Fixed, surprisngly it works after recreating the context xmls from scratch again (and probobly fixing typos)
Changes:
dispatcher-servlet.xml
<mvc:resources mapping="/resources/**" location="/resources" />
to
<mvc:resources mapping="/resources/**" location="/resources/" />
*.jsp
Using jstl "c" instead of spring
<c:url value="/resources/static/css/bootstrap.min.css" />
instead of
<spring:url value="/resources/static/css/bootstrap.min.css" />
However i think it was the first change that made it work.

Glassfish 3 - Loading images from a static server

I'm trying to load images (and other static content) from a server outside of my web application which is deployed to Glassfish v3. I have the following configs in the web.xml but it does not work on Glassfish (but it works on Tomcat):
<servlet>
<servlet-name>ExternalImagesServlet</servlet-name>
<servlet-class>com.example.servlet.HttpProxyServlet</servlet-class>
<init-param>
<param-name>RemoteURI</param-name>
<param-value>http://ip.of.second.server/website-files</param-value>
</init-param>
<init-param>
<param-name>AllowedContentTypes</param-name>
<param-value>image/gif,image/jpeg,image/png</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ExternalImagesServlet</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
Where ip.of.second.server is an actual IP address of the server. I have the file called website-files.xml defined as follow:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="d:/internet/website/images" />
And website-files.xml is saved to glassfish\domains\domain1\config directory. But Glassfish does not pick up this config file.
I have looked at Oracle Glassfish configuration doco but there's no mention on how you can reference images from a different server.
Please help.
I have solved it based on an old thread relating to Glassfish version 2 that I found on Google after two days of search.
In case anyone is interested in the solution, here it is:
1) Create a file called sun-web.xml directly under Webcontent\WEB-INF directory and add the following configuration to this file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
<sun-web-app>
<property name="alternatedocroot_1" value="from=/images/* dir=d:/internet/website" />
<property name="alternatedocroot_2" value="from=/files/* dir=d:/internet/website" />
</sun-web-app>
2) Remove the servlet and servlet-mapping configurations from web.xml file (like I did above). Note: The above would work if you were to use Tomcat.
3) Delete the website-files.xml from glassfish\domains\domain1\config directory as this file is not needed by Glassfish: Note: This file is needed by Tomcat.

No mapping found for HTTP request with URI: in a Spring MVC app [duplicate]

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I'm getting this error.
my web.xml has this
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
I have this in my web-application-config.xml
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<bean name="/Scheduling.htm" class="com.web.SchedulingController"/>
my com.web.SchedulingController looks like this
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class SchedulingController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView("/jsp/Scheduling_main.jsp");
modelAndView.addObject("message","Hello World MVC!!");
return modelAndView;
}
}
When I hit this controller with the URL http://localhost:8080/project1/app/Scheduling.htm
The Scheduling_main.jsp gets displayed but the images are not displayed properly. Also the js and css file are not getting rendered.
I'm accessing the images like this
<img src="jquerylib/images/save_32x32.png" title="Save Appointment">
If I change the URL mapping in the servlet definition to *.htm, the images get displayed fine. Can you point out where I'm missing out.
Here is complete error message
WARN [PageNotFound] No mapping found for HTTP request with URI [/mavenproject1/app/jquerylib/images/save_32x32.png] in DispatcherServlet with name 'springweb'
Thanks a lot.
Ravi
I'm think it happens because you try get your image though servlet (mapped as /app/*) You need get static content without handle it with your servlet, for example set image source to
<img src="../jquerylib/images/save_32x32.png" title="Save Appointment">
then real URI of your image will be /mavenproject1/jquerylib/images/save_32x32.png, and it will returned by your tomcat itself as is, without any processing.
I just add three rules before spring default rule (/**) to tuckey's urlrewritefilter (urlrewrite.xml) to solve the problem
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
<rule>
<from>/</from>
<to>/app/welcome</to>
</rule>
<rule>
<from>/scripts/**</from>
<to>/scripts/$1</to>
</rule>
<rule>
<from>/styles/**</from>
<to>/styles/$1</to>
</rule>
<rule>
<from>/images/**</from>
<to>/images/$1</to>
</rule>
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
How to handle static content in Spring MVC?
Add this to springweb-servlet.xml
<mvc:default-servlet-handler/>
Below text is extracted from Spring reference
This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping (given a lowest precedence order) of "/**". This handler will forward all requests to the default Servlet.

Resources