spring tools suite defaul mvc project web application - java

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.

Related

can't access maven project resource in spring MVC

the below is my configuration
project directory
the below image is my project structure. Spring Maven project
Servlet-context.xml
i have done resource mapping
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.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="/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.manjunath.controller" />
</beans:beans>
home.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="/resources/css/test.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>Home page:${pageContext.request.contextPath}</h1>
<h2 id="kaka">Hi this is manjunath</h2>
</body>
</html>
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/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/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 there is no error in project and successfully executed
but resource files are not mapping
I guess there is only 1 mistake in your code
In the page you need to give css link like below
<link href="resources/css/test.css" rel="stylesheet" type="text/css"/>
Just remove the / from href.

source javascript file in jsp with spring mvc <3.0.4

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.

Spring MVC - Getting constant 404 errors on Tomcat

I've created a basic Spring MVC web app with Maven. However, I've been struggling with getting constant 404 errors when I'm accessing the /login URL.
The /login URL should be mapped to the AuthController servlet and the createLoginForm() method, but unfortunately it fails and I'm ending up with a 404 error.
I'm trying to figure out what is issue that is causing 404 errors and why the /login URL cannot be mapped to the servlet.
Is the problem located in bad configuration of the web.xml or spring-dispatcher-servlet.xml files that prevent the URL to be mapped?
When I'm accessing the / URL, then the index.jsp file is mapped and it's is working fine
The login.jsp file is put outside the WEB-INF directory, in the webapp one as well as the index.jsp file.
Thanks in advance.AuthController.java
package com.github.wjoz.springmvcreview.auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class AuthController {
#RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
ModelAndView model = new ModelAndView("login");
return model;
}
}
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" 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">
<display-name>springmvcreview</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:default-servlet-handler/>
<context:component-scan base-package="main.java.com.github.wjoz.springmvcreview.auth" />
<mvc:annotation-driven />
<!-- Tells the location of the view in the project -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
login.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<title>Welcome to our application. Sign in.</title>
</head>
<body>
<form action="/springmvcreview/login" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<button type="submit">Sign in</button>
</div>
</form>
</body>
</html>
This is in addition to Shazin's answer
First, put your JSP files inside WEB-INF. There is no reason to have them outside of it, since you're deliberately breaking your view rendering functionality.
Second,
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
means that the URL www.somedomain.com/ will load the index page, and all other URLs will start with that. However, your form
<form action="/springmvcreview/login" method="post">
is hitting the URL www.somedomain.com/springmvcreview/login. This does not match up with the controller URL mapping
#RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
does not contain a mapping for the form action. So either remove /springmvcreview from the form action, or modify #RequstMapping value to /springmvcreview/login
You don't have the contextConfigLocation for your DispatcherServlet. You need that to tell context:component-scan to scan the com.github.wjoz.springmvcreview.auth package and load the AuthController.
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value><PATH_TO>/spring-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
I saw one more issue. You don't need the main.java part in the following;
<context:component-scan base-package="com.github.wjoz.springmvcreview.auth" />

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" />" />

How to use "css" in the "jsp" in Spring MVC project?

I am working on a spring MVC project in which I am trying to use css style sheets by referencing it to design my JSP page. But somehow my css files are not being picked up once I hit my method in the controller..
Below is my JSP file -
<html>
<head>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>
<body>
<h1>All header 1 elements will be red</h1>
<h2>All header 2 elements will be blue</h2>
<p>All text in paragraphs will be green.</p>
</body>
</html>
And below is my test.css file -
h1 {color:red;}
h2 {color:blue;}
p {color:green;}
And below is my method in controller class -
#RequestMapping(value = "testing", method = RequestMethod.GET)
public Map<String, String> testing() {
final Map<String, String> model = new LinkedHashMap<String, String>();
return model;
}
Directory structure is like this -
webapp/
|-- resources/
| +-- css/
| test.css
+- WEB-INF/
+-- views/
testing.jsp
But somehow it's not working for me.. Is there anything wrong I am doing here?
UPDATE:-
Here is my web.xml file-
<?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_3_0.xsd" 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">
<display-name>testweb</display-name>
<listener>
<listener-class>com.host.webres.resource.env.EbayResourceRuntimeListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index</welcome-file>
</welcome-file-list>
<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/context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/testweb/*</url-pattern>
</servlet-mapping>
</web-app>
And below is my context.xml file -
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:mvc="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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Allow proxys -->
<aop:aspectj-autoproxy />
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Support AJAX processing with progressive rendering. Overrides HttpOutputMessage with RaptorResponseWriter -->
<beans:bean class="com.host.terr.kernel.filter.RaptorJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.host.terr.config" />
<context:component-scan base-package="com.host.personalization.bullseye.zookeeper.p13nzook.controller" />
<!-- Handles HTTP GET requests by efficiently serving up static resources
in the corresponding directory -->
<resources mapping="/js/**" location="/js/" />
<resources mapping="/css/**" location="/css/" />
<!-- 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>
</beans:beans>
This is exception I am getting on my console but browser shows the data without any css -
404 /testweb/test.css Not Found /testweb/test.css Not Found }, Correlations : {} }
In your servlet-context.xml file,you have setting about your directory of resources.In your case,with changing
<resources mapping="/resources/**" location="/resources/"/>
and in your jsp page like below,
<link rel="stylesheet" type="text/css" href="
<c:url value="/resources/css/test.css"/> "/>
I hope it will work
You need to provide the context path.
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/test.css" />
What does this expression language ${pageContext.request.contextPath} exactly do in JSP EL?
You need to set <base/> as following...
<c:set var="req" value="${pageContext.request}" />
<c:set var="url"> ${req.requestURL} </c:set>
<c:set var="uri" value="${req.requestURI}" />
<head>
<title></title>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
Then You can Import any JS or CSS like below..
<script src="assets/js/angular/angular.js"></script>
Maybe by adding your .css file in a specific folder inside the WEB-Content then, you could use the attribute href = "yourFolder/yourFile.css
Indeed all "web" ressources like the view should be there (i.e. jsp, css.).
You can also precise that your link (in the .jsp file) is a stylesheet one by adding: rel="stylesheet"
you will have a link looking like this :
this css resource issue
please try below
<link rel="stylesheet" type="text/css" href="css/test.css" />
and you can see this resource if be load by bowaser.

Categories