source javascript file in jsp with spring mvc <3.0.4 - java

I know this has been asked, but that didn't solve my problem and have tried many things unsuccessfully:
My spring webapp project directory is pretty standard and looks like:
/webapp/js/* eg. /webapp/js/query.jeditable.js
/webapp/WEB-INF/jsp/*
/webapp/WEB-INF/web.xml+
/webapp/WEB-INF/spring-servlet.xml - dispatcher servlet config
/webapp/WEB-INF/spring-security.xml - spring security config
I am trying to source a javascript file in js from jsp url, which is also filtered through spring security url matcher.
Here is my web.xml:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>Welcome to projectBananaStand</display-name>
<description>
Welcome to 'projectBananaStand' Add Event Test
</description>
<context-param>
<param-name>jmxLogEnabled</param-name>
<param-value>false</param-value>
</context-param>
<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>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.jpmorgan.tyger.listeners.JMXLog4JContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>registerName</param-name>
<param-value>projectBananaStand</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/custError.jsp</location>
</error-page>
<distributable />
</web-app>
This is my jsp code trying to source js file:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>
<!-- editable datatables -->
<script src="/js/jquery.jeditable.js" type="text/javascript"></script>
<script src="/js/jquery.dataTables.editable.js" type="text/javascript"></script>
<script src="/js/addEvent.js" type="text/javascript"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.js" type="text/javascript"></script>
Here is spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<!-- declaring base package -->
<context:component-scan base-package="com.banana.controller" />
<mvc:annotation-driven/>
<!-- adding view resolver to show jsp's on browser -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value></property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean class="org.springframework.context.support.ResourceBundleMessageSource"
id="messageSource">
<property name="basename" value="messages" />
</bean>
</beans>
All app level beans and spring security related configuration has been defined in applicationContext.xml and added via contextListener
I cannot use
<mvc:resources mapping="/js/**" location="/js/" />
as I am using Spring 3.0.3 and don't want to upgrade whole framework.
I tried using just the xmlns definition for spring mvc 3.1 and that still caused the error - mvc:resource declaration not found.
I tried mapping a default servlet with url patterns
*.js, /js/**, and /somethingelse/*
and they broke the spring security default login page loading. Resource not found error for the spring security login page.
Tried different tags inside the jsp file including
source="<c:url value="something" />"
Please help, how can I include the javascript files in jsp without mvc:resource mappingor use mvc:resource mapping without changing all spring framework version and just using different
xmlns schemaLocationand not bring spring security with default servlet
Is there a way to include the files without treating them as urls? What am I doing wrong?
Help would be greatly appreciated as I have spent much time battling this.
Thanks

Keep your js folder into /WEB-INF/jsp/ folder it may be work i did not tried.

Related

Spring MVC is giving http 404 error what wrong?

i am learning spring mvc and i try to build a simple webapp but it don't work.
the error is : HTTP 404-NOT FOUND
i have try many time but it still the same error.
i don't know if the error is coming from my code or from tomcad 10
helps please
i am using eclipse IDE and tomcat 10
this is my folder structure
enter image description here
spring-mvc-demo-sevlet.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.luv2code.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>
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>
controller :
package com.luv2code.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
the view :
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2> Spring MVC Demo -Home Page</h2>
</body>
</html>

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.

Can't link .jsp with .css in spring web mvc

I have read most of the related posts I have found on this problem but I could not find any working solution for my situation.
Basicly my jsp file can't seem to find the css file.
Here are my files:
The jsp page:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Search for flight</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="${pageContext.request.contextPath}/WEB-INF/pages/theme.css" rel="stylesheet" type="text/css" >
</head>
the css page:
#title
{
font-family: "Times New Roman";
color: aqua;
}
mvc-dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</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>/</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>
</web-app>
And the controller works fine so I wouldn't find any real use in posting it.
The jsp file is visible from the browser but the styling is totally absent.
Thank you in advance!
you need to declare your resources in dispatcher-servlet file
<mvc:resources mapping="/resources/**" location="/resources/css/" />
Any request with url mapping /resources/** will directly look for /resources/css/
Now in jsp file you need to include your css file like this :
<link href="<c:url value="/resources/css/your file.css" />" rel="stylesheet">
complete example https://github.com/abdotalaat/SpringMVCCSS
I would suggest to add below lines in your spring configuration file(mvc-dispatcher-servlet.xml).
<mvc:resources mapping="/pages/*.css" location="/pages/"/>
<mvc:default-servlet-handler />
So I have resolved my problem.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
After adding this dependency the rest was pretty much flawless.
Thank you for your explanations! I have a much better understanding of how stuff works now.
I have the same problem:
- Check you add JSTL in pom.xml and declare in the jsp file
- Check with resources folders inside the webapp folder
- Check the href for your link css correct

spring mvc, css is not working

The css and javascript is not work on my page.
My home.jsp
<%#page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ru">
<jsp:include page="/WEB-INF/views/fragments/headTag.jsp"/>
<body>
<h2>Welcome!</h2>
</body></html>
The headTag.jsp
<%# taglib prefix="s" uri="http://www.springframework.org/tags" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<s:url value="/resources/css/test.css" var="testCss"/>
<link href="${testCss}" rel="stylesheet"/>
</head>
The web.xml
<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>spring.profiles.active</param-name>
<param-value>jdbc</param-value>
</context-param> -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>hibernate</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.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>classpath: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>
and my app servlet-comntext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- <Enable #Controller> -->
<mvc:annotation-driven />
<context:component-scan base-package="com.a72.coursapp.controller" />
<!-- resources mapping -->
<mvc:resources location="/resources/**" mapping="/resources/"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
The test.css
h2 {
color:red;
}
css location: src/main/webapp/resources/css/test.css
folder structure
-src
--main
---webapp
----resources
-----css
------test.css
-----js
-----fonts
----WEB-INF
-----views
------fragments
-------headTag.jsp
------home.jsp
What's wrong? Thanks in advance!
If look to source code page in firefox, page has test.ccs style, but it has this log:
Pivotal tc Runtime 3.0.0.RELEASE/8.0.9.B.RELEASE - Error report
HTTP Status 400 - description
The request sent by the client was syntactically incorrect.
Pivotal tc Runtime 3.0.0.RELEASE/8.0.9.B.RELEASE
I would have thought that your problem is the line:
<jsp:include page="/WEB-INF/views/fragments/headTag.jsp"/>
Don't think you can include anything behind WEB-INF with jsp:include.
It would anyway be rather unorthodox to call a view directly, they should be called by controllers.
Presumably if you are using spring you have a controller which calls that view? If so, then call the uri provided by the controller.
Otherwise I guess you need to move the jsp to a position that is not hiding behind WEB-INF.
Try this move your CSS and JS folder under WEB-INF
eg. if you are keeping your files like..
WEB-INF/resources/css/test.css
then change path in servlet-comntext.xml
<!-- resources mapping -->
<mvc:resources location="/WEB-INF/resources/" mapping="/resources/"/>
change in headTag.jsp page
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/test.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/test.js"></script>
Probably, you problem is base path. Try out:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link rel="stylesheet" href="<c:url value="/resources/css/test.css" />" />

spring tools suite defaul mvc project web application

hi i'm trying to change my default home page for another one but i couldn't do it, so i try to redirect the page from the default home page to the one i want, but i get this error
No mapping found for HTTP request with URI:
[/myapp/WEB-INF/views/index.html] in DispatcherServlet with name
'appServlet'
this is my setup i left everything by default when i create the project with spring tools suite
this is my servlet-contex.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="" />
</beans:bean>
<context:component-scan base-package="com.proj.myapp" />
this is my 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>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>
this is my deafult home file
home.jsp
enter code here
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
<c:redirect url="/indice.html"/>
</h1>
</body>
</html>
and this is the page i want to be redirected
enter code here
<html>
<body>
<h1>
INDICE
</h1>
</body>
</html>
and my contoller
#Controller
public class HomeController {
#RequestMapping(value = "/")
public String home() {
return "home.jsp";
}
#RequestMapping(value = "/indice.html")
public String indice() {
return "indice.html";
}
}
and i get this error
No mapping found for HTTP request with URI:
[/myapp/WEB-INF/views/indice.html] in DispatcherServlet with name
'appServlet'
As from your code suffix property value is blank so you need to pass the type of suffix to this property in in servlet-context.xml.
You have to change it
<beans:property name="suffix" value=".jsp" />
and your indices.html to indices.jsp then it will work.
But if you want to work it with html file but .html files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an mapping. This requires Spring 3.0.4+.
For example:
<mvc:resources mapping="/static/**" location="/static/" />
which would pass through all requests starting with /static/ to the webapp/static/ directory.
So by putting indices.html in webapp/static/ and using return "indices.html"; from your method, Spring should find the view.

Categories