Spring MVC Static Resource Sometimes Can't Find - java

First, Sorry about my poor english
=================================
My static resource in '/resource/images/test1/image1.png'
When "test1" folder is exist
I can call '127.0.0.1:8080/myproject/resource/test1/image1.png' find it
if i call '127.0.0.1:8080/myproject/resource/test1/image1.png' when folder not exist yet
of course i can't find it
but even i call url when after dynamically create folder
i still can't got image
EX1:
Call url before create folder and call again after dynamically create folder
------ Can't find, Only Get 404 Not Found
EX2:
Create folder before call url and call again
------ Can find
I thought it was a cache problem at first
But when EX1 problem occur, stall occur even change browser
Both EX are deployment on linux
I have the following Setting
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/base-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>signin</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
........
........
........
<mvc:annotation-driven />
<context:component-scan
base-package="com.chat.controller" />
<mvc:resources mapping="/resource/**" location="/resource/" cache-period="0"/>
<!-- jsp views -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

Related

SpringMVC how to return a .doc file (from a jsp page)?

I'm trying to map a request for a "fake" MSWord file: the file is an html page actually, with a .doc extension to force the user to download it and open it with MS Word.
The source file /app/report.jsp is populated and shown as an HTML page if the mapped URL does not contains a .doc extension, but if I add the extension all the <jsp:include /> in the jsp file stop working, and at the same time the file is still rendered as a html document.
How can I force the user to download the file as a Word document?
Here is the method mapping the URL, from my controller:
#RequestMapping(value="/report.doc", produces = "application/msword")
public ModelAndView reportProducer(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.addObject("something", false);
mav.setViewName("/app/report");
response.setContentType("application/msword"); // probably unnecessary?
return mav;
}
here is the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="3.1">
<display-name>MyApp</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/classes/jsp/app/errorPage.jsp</location>
</error-page>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
and here is my applicationContext.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<mvc:view-controller path="/" view-name="home"/>
<context:annotation-config/>
<context:component-scan base-package="myBasePackage1, myBasePackage2"/>
[... datasources, beans omitted ...]
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/classes/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
EDIT
The issue with the incomplete parsing of the page (not including *.jspf parts) is related to a change I did while trying to solve the issue. I removed the following code from web.xml. When restored, everything worked as expected:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspf</url-pattern>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
jspf files are normally JSP fragments, that are supposed to be included statically, not dynamically. I wouldn't use that standard file extension for regular JSPs. Just use the .jsp extension. That would make your custom jsp servlet mapping unnecessary, and would be less confusing.
WEB-INF/classes is for classes and resources loaded by the class loader. JSPs shouldn't be located there. I would put them anywhere else under WEB-INF.
Finally, regarding the rendering as a web page, that is because the default content type set by JSPs is text/html. You need to add
<%# page contentType="application/msword" %>
at the top of your JSP to set the appropriate content type.

Cant access view through direct URL [Maven]

I'm creating my first project in Maven, and already have a noob problem.
In my WEB-INF folder, I have another folder called "classes", in that folder I have a view called "view.jsp".
When I try to access the URL
http://localhost:8080/ProjectenIIJavaWeb/classes/view
I get a Http 404 not found error.
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
You have configured the DispatcherServlet only for the url pattern *.htm. So you have to call a URL with that pattern, otherwise there is no servlet which matches your request.
As the name says, the folder "classes" is for Java classes. Resources should be in the root of the WAR file.
You don't call your view, you define request mappings and let Spring MVC handle the view resolution. I suggest to read the Spring MVC documentation (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc)
In case you plan to keep your jsp's(without exposing the detailed path of actual file) inside WEB-INF this should help
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Detailed example: http://www.mkyong.com/spring-mvc/spring-mvc-internalresourceviewresolver-example/

Spring mvc doesn't see jsp

I'm creating a Spring MVC application and can't figure out why I get 404 error when launch my application. I want to see welcome page but it doesn't show. What is the issue?
In /WEB-INF/view there is index.jsp file with simple Hello text.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>grun</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/grun-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>grun</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
grun-servlet.xml
<context:component-scan base-package="Controller"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
Controller
#Controller
#RequestMapping(value="/")
public class AController {
#RequestMapping(method=RequestMethod.GET)
public String welcome() {
return "index";
}
}
I think the problem is this below bit in grun-servlet.xml
base-package="Controller"
Unusual to name a package as Controller with the uppercase 'C'. So unless you have
package Controller;
at the beginning of your AController.java file, that is a problem.
You can also try the following
WEB-INF flolder in generally where runtime libraries and class go. Create view folder inside Webcontent folder and provide path as
<property name="prefix" value="/view/"/>
Wecontent is your root folder for all the resources you want to expose. Reason was doing above is that as Webcontent is the root folder to expose web resources directories inside it can be specified relative to this. So Any resource abc under folder xyz can be accessed using /abc/xyz.
Nothing is wrong with your configuration, Just add Index.jsp as a welcome file on you web.xml and it should works
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Please change your web.xml to this
<servlet>
<servlet-name>grun</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>grun</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/grun-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Hope it will work fine
2 possible things:
Did you try putting the request mapping at the method level instead of at the Controller level?
#Controller
public class AController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index";
}
}
Try adding the below to your grun-servlet.xml:
<context:annotation-config />
<context:component-scan base-package="com.xxx.yyy" />
where "com.xxx.yyy" is the base package to which your AController belongs to.

I/O failure during classpath scanning in spring MVC

Hi I am new to Spring and I am trying to do a simple spring mvc application which accepts values from customer form and displays the same in the same in the UI. Now the problem is when I try to publish the code in Jboss server I am getting the following error message:
14:40:31,555 ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure during classpath scanning; nested exception is java.io.FileNotFoundException: D:\jboss-5.1.0.GA\server\default\deploy\SpringMVC.war\WEB-INF\classes\com\spring\customer\controller (The system cannot find the path specified)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:222)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:201)
at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:69)
Here is my web.xml:
<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>SpringMVC</display-name>
<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>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/pages/404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/pages/404.jsp</location>
</error-page>
</web-app>
And the Dispatcher Servlet code is as follows:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.spring.customer.controller" />
<bean class="com.spring.customer.validator.CustomerValidator" />
<!-- Register the Customer.properties -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/spring/customer/properties/Customer" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
I tried searching in google but could not find what is wrong. I have been breaking my head for this issue for almost 2 hours. Some one help!!
P.S: I am using maven.
Thanks in advance
There is :
<context:component-scan base-package="com.spring.customer.controller" />
and it seems you don't have such package in you application, but Spring is searching this package (so a directory) to find components.
Please verify that in binaries you have such package (or maybe you don't have this package at all?)

How to map content properly in web.xml for a Spring application?

My web.xml contains
<?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" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
resources/index.html
</welcome-file>
</welcome-file-list>
</web-app>
The resources/index.html references to other static resources like images, js, css etc. stored in resoruces directory by relative paths.
When I put http://localhost/MyProject/ in browser, it showed up index.html but didn't get the css and javascripts.
However, if I put http://localhost/MyProject/resources/index.html in the browser, everything shows up correctly.
So, the question is how can I let the welcome page served in the url as the path given in the <welcome-file>, e.g. /resources/index.html.
If it can't be done in the <welcome-file list>, what other configurable method should I use.
I tend not to redirect to /resources/index.html by adding another html or by doing it programmatically in a Servlet controller.
It seems you are using Spring and having problems with static content.
Try looking at this link
It explains how to proceed in this case...
Pay atention to the line:
<mvc:resources mapping="/resources/**" location="/resources/" />
It maps your resources folder (containing the css, javascript and image files) to a special handler of Spring.
Update:
In your servlet-context.xml file you can add this line
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Resolves view names to protected .html resources within the /WEB-INF/resources/ directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/resources/"/>
<property name="suffix" value=".html"/>
</bean>
that says that you don't have to use the 'index.jsp' properly. This way, you will map a view to the "/" access. Summarizing, this way the user enters in 'http://localhost/MyProject/' and sees your index.html and sees the effects of css and javascripts.
PS.: - This configurations only works on Spring 3+
- Prefer naming your files to '.jsp' and not '.html'... it is simpler to map.

Categories