Spring MVC CSS/JS not found - java

In my maven Spring Project in netbeans CSS/Js file not opening ..
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" version="3.0">
<display-name>OrderManager</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringConfiguration</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringConfiguration</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
SpringConfiguration-servlet.xml
e<?xml version="1.0" encoding="UTF-8"?><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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ordermanager.users.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/**"/>
Project Folder
i cant able access any css or JS file even after controller and other function working fine ..
but if i open the following url in browser
http://localhost:8080/OrderManager/resources/css/loginpagestyle.css, it's showing not 404 found.

After much search, my 404 issue was resolved by removing the initial "/" in the location setting value. So instead of
<mvc:resources mapping="/resources/**" location="/resources/"/>
as in the previous answer, I used:
<mvc:resources mapping="/resources/**" location="resources/"/>
Small change, but it did the trick. I'm assuming the "/" added an additional folder level to the path. Also, I placed the resources folder directly under the webapp folder.
Thank you to Suda's Tech Zone for helping me with this issue.
Using Maven 4.0.0, eclispse kelper, java 1.7, mybatis 3.1.1.

Your resource mapping is wrong. Should be:
<mvc:resources mapping="/resources/**" location="/resources/"/>

Related

java spring mvc 404 not found error for sample Spring MVC

I am learning spring MVC and getting the below problem.Even I am not able to run any sample application downloaded from internet as well.
I am getting 404 Not Found "Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
I have tried the below
Adding the Maven dependencies in Deployment Assembly.
Changing the Tomcat server path.
#Controller
public class ControllerClass{
#RequestMapping("hello")
public String redirect() {
return "Hello";
}
}
This is my Controller class
<?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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="com.practice.controllerclasses" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
is my DispatcherServletConfiguration xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<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>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
is my web.xml file
I am using Eclipse JEE 2019-09 Jre1.8 and Spring 4.1
Any help is appreciated.

Spring MVC project only redirected to tomcat page instead using intellij. How to solve?

I am using spring framework 5.0.4 and tomcat server 9.0.2.
And its a maven project.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/cache/spring-tx.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/>
<context:component-scan base-package="com.project.springbase"/>
<!-- This is the location under /webapp/resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Can also use the default maven resources folder with subfolder name-->
<!--<mvc:resources mapping="/resources/**" location="classpath:/foo/" />-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
</beans>
applicationContext.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: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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/tx/spring-context-4.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/datasource-cfg.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
</beans>
Here is my intellij server console when deploying project:
And here is the browser output:
Everytime i deploy or run the project this page comes instead of my index.
And here is my home controller along with the project strcture:
With all my configurations, whenever i run the project, it shows the tomcat page on localhost:8080 instead of my project index.jsp.
What could be the possible reason?
Try this tutorial, it helped me. also change change your port number
Step 1 create Spring project on IntelliJ
Additional libraries
Spring MVC and
Java EE: Web Application
Step 2
in web.xml
- change
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
- to
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
Step 3
Edit your dispatcher-servlet.xml
<context:component-scan base-package="com.springdemo" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
Step 4
Create your Controller.java
Step 5
Add Tomcat Server
Run > Edit Configuration > Click "+" > Tomcat Server > Local
Still inside Run/Debug Configurations dialog box
Warning: No artifacts marked for deployment > Click Fix
on Application context: "/" remove name_war_exploded
under Server I change my URL tohttp://localhost:8082/
Also on HTTP Port: 8082
Step 6
File > Project Structure > Problems (2) >
Click Fix > add all all missing dependancies
Step 7
delete index.jsp in directory web
create a new index.jsp in directory web > WEB-INF > views
In Run Configuration you have following section:
So you either need to uncheck the After Launch action or change URL to http://localhost:8080/index.jsp or whatever else.

Getting Error(HTTP Status 404 -) in Spring Mvc

I am new in SpringMVC and trying to execute simple hello world program. However, when run this in my browser (http://localhost:8080/FirstspringMVCwithannotation/welcome) I got HTTP Status- 404 Error. Here, is the code:
HelloController.java
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class HelloController {
#RequestMapping("/welcome")
public ModelAndView helloWorld(){
ModelAndView model=new ModelAndView("HelloPage");
model.addObject("msg","hello world");
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>FirstspringMVCwithannotation</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.example" />
<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>
</beans>
HelloPage.jsp
<html>
<body>
<h1>
First Spring MVC Application Demo
</h1>
<h2>
${msg}
</h2>
</body>
</html>
Here is my project structure and I added all spring jar files under lib folder
I tried to see other solution but that does not solved my problem..can anyone help me please, why i get HTTP Status- 404 Error? Thanks in advance
You received HTTP 404 because your dispatcher controller can't find you view.
Your view is under lib folder, which is under WEB-INF folder.
All you need to do is the reconfigure you view resolver, instead of what you wrote:
<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>
write it like this: configure your view resolver to extract views from lib folder
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/lib/" />
<property name="suffix" value=".jsp" />
</bean>
One more thing, I would recommend that you place your jsp files under views folder under WEB-INF and not under lib folder.
lib under WEB-INF is a folder dedicated to 3rd party libraries and jars.

Java Spring MVC ScriptTemplateViewConfigurer can't find javascript resources

I'm trying to create a small test webapp with javascript based templates. Since Spring 4.2.0 there is a special ScriptTemplateViewResolver class for this. While deploying my app on local tomcat (8.0.21) server, I'm getting the following exception:
java.lang.IllegalStateException: Resource /WEB-INF/resources/js/lib/ejs.min.js not found
at org.springframework.web.servlet.view.script.ScriptTemplateView.loadScripts(ScriptTemplateView.java:234)
Here is webapp directory structure:
src/main/java/com/itsl/mercado/pages/PagesController.java <-- here my controller lives
src/main/webapp/WEB-INF/pages/index.ejs <-- index page template
src/main/webapp/WEB-INF/pages/index.jsp <-- index.jsp is here also (for debug experiment)
src/main/webapp/WEB-INF/resources/js/lib/ejs.min.js <-- javascript libraries (EJS in my case)
src/main/webapp/WEB-INF/resources/js/render.js <-- javascript render function
src/main/webapp/WEB-INF/pages-servlet.xml <-- servlet context config
src/main/webapp/WEB-INF/web.xml <-- here web.xml lives
Here is web.xml:
<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_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>pages</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pages</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And pages-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.itsl.mercado.pages"/>
<mvc:view-resolvers>
<mvc:script-template prefix="/WEB-INF/pages/" suffix=".ejs"/>
</mvc:view-resolvers>
<mvc:script-template-configurer engine-name="nashorn" render-function="render">
<mvc:script location="/WEB-INF/resources/js/lib/ejs.min.js" />
<mvc:script location="/WEB-INF/resources/js/render.js" />
</mvc:script-template-configurer>
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--<property name="prefix" value="/WEB-INF/pages/"/>-->
<!--<property name="suffix" value=".jsp"/>-->
<!--</bean>-->
</beans>
The strange thing is that when I comment ScriptTemplateViewResolver and uncomment InternalResourceViewResolver it manages to find index.jsp, but, however, it fails to find /WEB-INF/resources/js/lib/ejs.min.js
Also, I've tried following in pages-servlet.xml (without any other result):
<mvc:annotation-driven />
<context:component-scan base-package="com.itsl.mercado.pages"/>
<mvc:view-resolvers>
<mvc:script-template prefix="/WEB-INF/pages/" suffix=".ejs"/>
</mvc:view-resolvers>
<mvc:script-template-configurer engine-name="nashorn" render-function="render">
<mvc:script location="/resources/js/lib/ejs.min.js" />
<mvc:script location="/resources/js/render.js" />
</mvc:script-template-configurer>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<mvc:default-servlet-handler />
If anyone has an idea, please, help me to figure out how to deal with the problem. Thanks in advance.

HelloWorld SpringMVC - HTTP Status 404

Im trying to create my first Spring MVC project but I keep getting error http status 404.
If i try to open an .jsp file outside of /views (So not using Spring MVC) it works fine
Here are my files:
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>fj21-tarefas</display-name>
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-context.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.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="br.com.caelum.tarefas" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller
package br.com.caelum.tarefas.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class OlaMundoController {
#RequestMapping("/olaMundoSpring")
public String execute(){
System.out.println("Executando a lógica com Spring MVC");
return "ok";
}
}
Libs
aopalliance-1.0.jar
commons-logging-1.2.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1-3.jar
jcl-over-slf4j-1.7.12.jar
joda-time-2.4.jar
log4j-over-slf4j-1.7.12.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.1.7.RELEASE.jar
spring-aspects-4.1.7.RELEASE.jar
spring-beans-4.1.7.RELEASE.jar
spring-context-4.1.7.RELEASE.jar
spring-context-support-4.1.7.RELEASE.jar
spring-core-4.1.7.RELEASE.jar
spring-expression-4.1.7.RELEASE.jar
spring-instrument-4.1.7.RELEASE.jar
spring-instrument-tomcat-4.1.7.RELEASE.jar
spring-jdbc-4.1.7.RELEASE.jar
spring-jms-4.1.7.RELEASE.jar
spring-messaging-4.1.7.RELEASE.jar
spring-orm-4.1.7.RELEASE.jar
spring-oxm-4.1.7.RELEASE.jar
spring-test-4.1.7.RELEASE.jar
spring-tx-4.1.7.RELEASE.jar
spring-web-4.1.7.RELEASE.jar
spring-webmvc-4.1.7.RELEASE.jar
spring-webmvc-portlet-4.1.7.RELEASE.jar
spring-websocket-4.1.7.RELEASE.jar
When your controller method returns a String, the String to be returned is the name of your view (jsp, template, etc).
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
According to the setup above (provided by you), Spring in intended to return a view located on /WEB-INF/views and the name of the view must has the suffix ".jsp".
Please, make sure the file "ok.jsp" is located inside this path, otherwise, Spring will not be able to load it.
In case "ok.jsp" is already located in this folder, please, provide us full stacktrace of the exception you are facing.
Yes, the view "ok.jsp" is located on the corrected folder.
I ended up fixing the issue by deleting all the libs and adding the dependency using maven.
If you are using spring, you should us spring boot to get started with any new service. Takes care of your basic dep tree. Refer to the tutorial at - http://projects.spring.io/spring-boot/#quick-start
Pretty straight forward.

Categories