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.
Related
When I run the web application I should see some information about the dispatcher servlet in the console but I only get information about the TomCat server. The Spring dispatcher servlet never get's loaded as it should. I've tried everything for about a week now but I still could not get it working. Here is some code.
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>
<!-- 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/mvc-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>
mvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="pack"/>
<!-- 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>
The output in my console shows only info about TomCat:
My console output
I need to see this info message:
Correct console output
I actually found an answer, this is very very specific problem for those who are following this udemy course from love2code.com. The problem was that the instructor used some faulty jar files that worked back when he made the tutorial but do not work today. The solution is to add the spring-web and spring-webmvc dependencies to your pom.xml. This solved my silly problem. Special Thanks to Chad Darby (udemy instructor) for wasting my time with these faulty jars.
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.
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.
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/"/>
I am having a major problem with paths in my Spring MVC project.
I have fixed so that I can see images/Css and so on in my jsp files, but my html files I am at a loss, I have tried all kinds of paths I can think of, but nothing seems to work... I hope anyone of you can help me find what I am doing wrong.
My spring config file:
<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-2.5.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="controller" />
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="resources/"></mvc:resources>
<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>
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"
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>Project By Robin Ejderholt</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
The css code im trying to fix:
<link href="/resources/css/menucss.css" rel="Stylesheet" type="text/css" />
The current structure of my project:
Hope anyone can help because I got no clue why it doesn't work.
PS: I am using jstl on the jsp and that works, but what I am aware it is not possible on static html files.
Thanks
EDIT UPDATE:
I solved it, the solution for people with the same problem was to use the relative path; "../map/filename" instead of "/map/filename".