I cannot access files in resource directory. What is the problem?
in servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.example.test" />
</beans:beans>
in 'HomeController.java'
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
File file = new File("/resources/sample/text.txt");
if (file.exists()) {
logger.info("file found.");
} else {
logger.info("file not found.");
}
return "home";
}
in web.xml (default)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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>
</web-app>
Oups, that is not the correct way to access resources in a web application.
First, your resource paths are relative to the ServetContext path, so you should not try to use them as file system absolute paths. But in addition, you cannot be sure that they exist as true files.
The servlet specifications allow a servlet container not to explode a war file, prodided it is able to correctly serve resources through ServletContext.getResourceAsStream(java.lang.String path)
That means that you should only use resources as read-only through ServletContext.getResourceAsStream(java.lang.String path) and never try to use them as true files.
Related
Here's the spring-mvc-demo-servlet.xml:
<?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">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.robert.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Here's the web.xml:
<?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"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Here's the Controller java file
public class HelloWorldController {
// need a controller method to show the initial HTML form
#RequestMapping("/showForm")
public String showForm() {
return "helloworld-form";
}
}
When I try to run it on the server I'll get this error:
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /spring-mvc-demo/showForm
I'm new to SpringMVC, I thought it has something to do with component scanning but I've added component-scan in the xml file.
Thanks.
Try this:
#RequestMapping("/spring-mvc-demo")
public class HelloWorldController {
// need a controller method to show the initial HTML form
#RequestMapping("/showForm")
public String showForm() {
return "helloworld-form";
}
}
With your current setup you need to hit GET /showForm then it will work.
Please also check other ways of mapping like, GetMapping, PostMapping, PutMapping, DeleteMapping, PatchMapping and others
Changing the original JRE library to JRE System Library in eclipse
In my project using Spring MVC and JSON, I configure my web.xml file following as:
Web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<!-- Processes application requests -->
<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>
My Controller:
#Controller
public class HomeController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
#RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
public #ResponseBody List<Person> personsReturn(#RequestParam("name") String name, #RequestParam("age") int age) {
Person p1 = new Person("dat1", 11);
Person p2 = new Person("dat2", 22);
Person p3 = new Person("dat3", 33);
Person p4 = new Person("dat4", 44);
List<Person> lists = new ArrayList<Person>();
if (name.equals("dat")) {
lists.add(p1);
lists.add(p2);
} else {
lists.add(p3);
lists.add(p4);
}
return lists;
}
}
My Servlet-Context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.luongkhanh.restfulspringmvc" />
</beans:beans>
I can get data from JSON by URL:
http://localhost:9999/restfulspringmvc/json?name=dat1&age=11
Result JSON:
[{"name":"dat3","age":33},{"name":"dat4","age":44}]
But If I want to get the image by URL at location, it's unsuccessful:
http://localhost:9999/restfulspringmvc/images/avatar.jpg
Logger in eclipse alert an exception:
ARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/restfulspringmvc/images/avatar.jpg] in DispatcherServlet with name 'appServlet'
How to configure to get image from URL by Browser? Thank you so much !!
You have configured your DispatcherServlet to use the url-pattern / This overrides the default servlet mapping of the container.
The default servlet mapping is used to load any resources or requests without an explicit mapping in web.xml. Typically static resources are loaded using this. If you override it by specifying it as a pattern for your servlet in web.xml then you have to take care of loading the resources yourself
Spring MVC helps by providing resource handling via
You have to specify the mapping which matches your static resources url pattern and the location where your resources reside physically
You did specify
<resources mapping="/resources/**" location="/resources/" />
but your image does not reside in a resources folder under webapp Maven folder. Instead its in images folder. Hence you have to add another resources tag like this
<resources mapping="/images/**" location="/images/" />
NB The mapping attribute and the location attribute does not have to correspond. For example you can have a url like
http://localhost:9999/restfulspringmvc/images/avatar.jpg
and your images are in /static-resources/images under webapp the your mapping will be
<resources mapping="/images/**" location="/static-resources/images/" />
You can also have multiple resources specification in the same application context
I am trying to load my Spring application and I have only html files to render but it is not able show any html file. It is always showing 404 error. But when I am trying to run it directly html file using tomcat then it will able to show my html files.
Below is my directory structure
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>ankServlet</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>ankServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<context:annotation-config />
<!-- Resolves views selected for rendering by #Controllers to .htmlresources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/resources/html/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
<context:component-scan base-package="com.ankit" />
</beans:beans>
HomeController.java
#Controller
public class HomeController {
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String displayLogin(HttpServletRequest request, HttpServletResponse response)
{
return "login";
}
Try to change this:
<servlet-mapping>
<servlet-name>ankServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To:
<servlet-mapping>
<servlet-name>ankServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
My app works fine on local tomcat 7 but on the prod tomcat 7 only index url ("/") and resources urls works, other urls are not found, "/login" for example.
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<!-- FreeMarker Config -->
<beans:bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<beans:property name="templateLoaderPath" value="/WEB-INF/views/" />
</beans:bean>
<!-- FreeMarker View Resolver -->
<beans:bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<beans:property name="cache" value="true" />
<beans:property name="prefix" value="" />
<beans:property name="suffix" value=".html" />
</beans:bean>
<context:component-scan base-package="com.x.y" />
</beans:beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<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>
</web-app>
Trying to resolve I changed the servlet-mapping from <url-pattern>/</url-pattern> to <url-pattern>/*</url-pattern> and I got no success, but it started to find the urls if I put the suffix ".jsp", eg: /login.jsp.
Can anyone help me understand why the tomcat is waiting this ".jsp" suffix? I'm not even using jsp in my app :s
Controller sample:
#Controller
public class Application extends BaseController {
//...
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model, HttpSession session) {
//..
return "application/index";
}
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, HttpSession session) {
//..
return "application/login";
}
//...
}
Thanks.
Sounds like you might have apache sitting in front of tomcat in your production environment, and it is only passing through certain url's, eg: *.jsp
Alternatively, it could be a firewall blocking certain URL's.
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 absolutely newbie with Java and Spring and I want to learn from example.
I'm using an out-of-the-box configuration / installation
Mac OSX
Springsource Tool Suite as IDE
Spring 2.8.1.RELEASE
vfabric-tc-server-developer-2.6.1.RELEASE
I've tried to generate a new project based on "Spring Template Project". Then I've chosen the "Spring MVC Project". The sample project is generated. After that, without modifying anything, I've tried to execute de "home.jsp" page via "Run As". The Web Server starts and finally I received the error in the console Tab.
No mapping found for HTTP request with URI [/myproject/] in
DispatcherServlet with name 'appServlet'
And this other output in these web pages:
http://localhost:8080/myproject/WEB-INF/views/home.jsp
http://localhost:8080/myproject
Here you can see an image on how my project is structured (auto generated for STS):
What is wrong?
Here you can see the content of the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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>
</web-app>
The root-context.xml file has this content.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
</beans>
And finally the servlet-context.xml has this content.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.mycompany.myapp" />
</beans:beans>
Does anybody have an idea to solve it?
Spring's convention is to assume that the <servlet-name> in the web.xml for the DispatcherServlet matches the beginning of the Spring servlet context XML file. Rename servlet-context.xml to appServlet-context.xml and see if that helps.
Everything under WEB-INF is not accessible from the outside, and the point of an MVC framework is to invoke a controller before dispatching to the view, so invoking the JSP directly should not be done anyway.
And you probably don't have any Spring controller mapped to the root URL, so obviously, there is nothing at the URL http://localhost:8080/myproject/.
Add a Controller to your application that returns a ModelAndView instance with name "home". Then configure some handler mapping to that Controller and try to access your web app with a URL similar to this one: http://localhost:8080/myproject/home.do. More information can be found here and here.