link jQuery file with Spring MVC - java

I have an issue in linking jQuery file, have placed the .js file under
WebContent
--js
-----toggle.js
file structure is shown in... (http://i.imgur.com/oRzM0tP.png)
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" 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>FirstSpringMVCProject</display-name>
<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
<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">
<context:component-scan base-package="com.gontuseries.studentadmissioncontroller" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:annotation-driven/>
<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>
but .js file is not linked up, please give me a directions, thanks in advance!!!

I think you should put ${pageContext.servletContext.contextPath} and you should notice the name (and version ) declare in Spring-dispatcher-servlet.xml must same with the file in /js directory (jquery-1.6.2.js,jquery-1.11.1.js, jquery.js v.v...)
<script type="text/javascript" src="${pageContext.servletContext.contextPath}/js/jquery-1.6.2.js"></script>
and as everyone suggest, make sure you have
<mvc:resources mapping="/js/**" location="/js/" />
in Spring-dispatcher-servlet.xml
hope it work

Its not a very much spring way, but I could link a javascript file as below
<script type="text/javascript" src="/js/jquery-1.11.1.js" ></script>
Please check if it works for you also.!

Related

404-resource not found in MVC

404-resource not found in MVC using spring
there is no error in any of the file and the code is also correct still getting error. i checked it many times.
below are my web.xml, *-servlet.xml and my beans.xml file
*Beans.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
<bean id="product" class="com.shoppingcart.beans.Product">
<property name="id" value="10"></property>
<property name="name" value="Apple"></property>
</bean>
<bean name="vendor" class="com.shoppingcart.beans.Vendor">
<property name="name" value="iStore"></property>
<property name="city" value="Thane"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
</bean>
<bean name="account" class="com.shoppingcart.model.Account"></bean>
<context:component-scan
base-package="com.shoppingcart.beans, com.shoppingcart.model, com.shoppingcart.controller">
</context:component-scan>
<context:annotation-config></context:annotation-config>
<context:property-placeholder
location="com/shoppingcart/main/jdbc.properties" />
</beans>
----------------------------------------------------------------------------
404-resource not found in MVC using spring
there is no error in any of the file and the code is also correct still getting error. i checked it many times.
below are my web.xml, *-servlet.xml and my beans.xml file
*shoppingcart-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
<context:component-scan base-package="com.shoppingcart.controller">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
----------------------------------------------------------------------------404-resource not found in MVC using spring
there is no error in any of the file and the code is also correct still getting error. i checked it many times.
below are my web.xml, *-servlet.xml and my beans.xml file
*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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ShoppingCart</display-name>
<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-list>
<servlet>
<description></description>
<display-name>shoppingcart</display-name>
<servlet-name>shoppingcart</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>shoppingcart</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Where is the index.* files mentioned in the welcome-file tag placed ?
when the application starts, it is checking the welcome-files mentioned in web.xml inside WebContent folder . But if you have placed them inside WebContent/jsps/ the requested files wont be found. That can cause a 404.
Solutions:
1.You can move your jsps to WebContent directory.
2. In the welcome-file tag give the complete path of the index.jsp
Example : <welcome-file>/jsps/index.jsp</welcome-file>
3.Since you are using Spring and an InternalResourceViewResolver you can also configure a controller to load the welcome page instead of moving the files.
Example :
#RequestMapping(value="Login.jsp")
public ModelAndView showLoginPage(HttpServletRequest request, HttpServletResponse response ){
return new ModelAndView("Login");
}
then accessing the page using the url:
http://localhost:port/context_root_of_your_application/Login.jsp
Dispatcher Servlet will pick this call -- redirect to controller -- look for the view ("Login" ) inside /jsps/ and if matching jsp is found it will redirect to that jsp.
Hope this helps

spring render welcome file like a text

I am new in spring, my issue is that index.jsp show me like text.
this is my web.xml
I use netbeans, spring 4, glassfish and maven
<?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">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
spring-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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
">
<mvc:default-servlet-handler/>
<context:annotation-config />
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/bootstrap/**" location="/resources/" />
<mvc:resources mapping="/js/**" location="/js/" />
<context:component-scan base-package="mx.com.shopping.cart.spring.controller" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
my tree directory
but show
browser
Let your spring dispatcher map your urls. So in your web.xml change your welcome-file to:
<welcome-file>/</welcome-file>
then in your controller add a mapping for index.
#RequestMapping(value = "/", method = RequestMethod.GET)
public String viewIndex() {
return "index";
}
And put your index.jsp inside your WEB-INF/jsp folder

Including css and js file in spring mvc 3 [duplicate]

This question already has answers here:
Spring 3 MVC resources and tag <mvc:resources />
(10 answers)
Closed 7 years ago.
I have searched on Google for serveral hours , but I can figure out this . When I try to access css/js file both inside and outside WEB-INF . But Tomcat just shows me 404 error .
dispatcher-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/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">
<context:component-scan base-package="controller" />
<mvc:annotation-driven />
<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>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
In my index.jsp file , I try all of these
<link rel="stylesheet" href="<c:url value="/resources/css/site.css"/>"/>
<link rel="stylesheet" href="<c:url value="${pageContext.request.contextPath}/resources/css/site.css"/>"/>
<link href="/resources/css/site.css" rel="stylesheet">
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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"
metadata-complete="false">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.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>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
</web-app>
Your configuration files map resources in webapp/resources, while you put it into WEB-INF. To maintain your directory tree you should use
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

MIME type text/html

Hi I'm working on a project and try to add css to a page but its not loading and when i check srourcecode at the browser I get a :
Resource interpreted as Stylesheet but transferred with MIME type text/html:
I tried to load css in every way, with
- " rel="stylesheet">
-
- even with #import file ( not a best practice but i tried...)
and its not working.
Files:
Project:
-WebContent
-resources
- css
-main.css
-WEB-INF-
-MTEA-INF
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/springapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
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.drs.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="resources/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
Anyone have an ideea?How to solve this?

Using apache velocity for view

i downloaded a sample application from RoseIndia http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc-hello-world.html and it works fine, but i want to use apache velocity instead of jsp. what changes should i need in the current project.
Note: I have changed the suffix value to ".vm" instead of JSP. and also view resolver to
"org.springframework.web.servlet.view.velocity.VelocityViewResolver"
i could not get the desired result, i got resource not found message.
Here is web.xml file content
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_5.xsd">
<display-name>RoseIndia</display-name>
<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>/forms/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
and servlet file contents are
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="net.roseindia.controllers"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Thanks in advance,
Declare 2 additional beans
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">/WEB-INF/views/</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"/>
, and remove
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">/WEB-INF/views/</property>
<property name="suffix">.jsp</property>
</bean>

Categories

Resources