Import extJs to JSP - spring

I am using Spring tool suite and I created MVC application. Now I'd like to move to ExtJs with this concept.
But I am not able to include extJs files into JSP.
My servlet looks like this:
<mvc:resources mapping="/resources/**" location="/resources/" />
<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>
<context:component-scan base-package="net.codejava.spingextjs" />
my JSP looks like this:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page session="false" %>
<html>
<head>
<link href="resources/css/ext-all.css" rel="stylesheet">
<script src="extjs/ext-debug.js" type="text/javascript"></script>
<script src="app/app.js" type="text/javascript"></script>
</head>
<body>
</body>
Folder tree looks like this:
However page won't load and firelogger says that /extjs/ext-debug.js and app/app.js is not located on server (404 on these files). I use tomcat and trying it on localhost.
So my question is, what should I do to import ExtJs in JSP, or should I change home.jsp to home.html (tried this, didn't helped)?
I already made the ExtJs app using static web page and loading in html, however I am not able to force server to load ExtJs using MVC concept.

Your js/css/images need to be located in the resources folder.
There are a bunch of examples of using ExtJS with Spring MVC out there:
https://github.com/loiane/extjs-crud-grid
It relies on an older version of Ext but it's a nice boilerplate.
Cheers

You may need to MOVE extjs/ext-debug.js and app/app.js into the resources directory because the other locations will be handled by a dispatcher servlet.
OR, if you do not want to move these files, then add two line in your servlet configuration.
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/extjs/**" location="/extjs/" />
<mvc:resources mapping="/app/**" location="/app/" />
<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>
<context:component-scan base-package="net.codejava.spingextjs" />
If it does not solve the problem after you have moved or added two lines in the configuration file, then try the jstl c:url tag like below.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page session="false" %>
<html>
<head>
<link href="<c:url value='/resources/css/ext-all.css' />" rel="stylesheet">
<script src="<c:url value='/extjs/ext-debug.js' />" type="text/javascript"></script>
<script src="<c:url value='/resources/css/ext-all.css' />app/app.js" type="text/javascript"></script>
</head>
<body>
</body>

Related

spring mvc - how to remove the word 'resources' when calling js,css,images

I have a resources folder in my WebContent folder that contains css, js, and images... Then i declare mvc resource mapping to locate all the resources file... but when i tried to call/acccess the file in my jsp, why is that i need to include the word resources?... do you know how can i remove it but still able to access my resources?
project structure
WebContent
|_resources
|_images
|_js
|_WEB-INF
|_jsp
|_header.jsp
header.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="<c:url value="/resources/js/jquery.min.js" />"></script>
dispatcher.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
above code works perfectly, but i want to exclude the word resources in path..
without writing one by one all the inside resources folder (like sample below)
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
what i need is something like this... but still declaring one line mvc resource mapping
<script src="<c:url value="/js/jquery.min.js" />"></script>
<link rel="stylesheet" href="<c:url value="/css/owl.carousel.min.css" />">
<img src="<c:url value="/images/pix/world.jpg" />" alt="user">
Do you use the server is tomcat? If you use the server is tomcat, you can do the following configuration
<Url-pattern> / </ url-pattern> means to intercept all requests.
If you need to deal with static resources js, css, etc., you can add in web.xml written before dispatcherServlet
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
Default is Tomcat's default servlet!

SpringMVC - cann't able to acces JS and CSS file in index.jsp

I have added
<mvc:resources mapping="/webapp/**" location="/webapp/" />
<mvc:default-servlet-handler />
in my dispatch-servlet.xml
my folder hierarchy is
enter image description here
and in index.jsp i declared
<spring:url value="/webapp/resources/js/bootstrap.min.js" var="bootstrapJS" />
<script src ="${bootstrapJS}" type="text/javascript"></script>
can any one tell the actuall issue is with code or folder hierarchy
Hi have you included the tag at the top of index.jsp it will look something like this. <%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>

How to display images in a jsp file in Spring MVC

I am very new to spring. I am using spring 3.29 version, tomcat 7. I need to display an image in .jsp file. I searched a lot. There are plenty of post about this problem. But still i am unable to solve this problem. Please help.
The following is my application structure
The following is my spring-servlet.xml file code
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.wipro.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
I have a page footer.jsp. I need to add an image in this file
The following is code
<!-- Here I need to add an image -->
<hr/>
<p>Copyright 2010-2014 javatpoint.com.</p>
I got the image in my page. #Pankaj Saboo comments helped a lot. Thanks for all of you.
I have added the below code in spring-servlet.xml
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="WEB-INF/resources/images/" />
And in footer.jsp I have added the below code
<img src="<c:url value="/images/wipLogo.png" />"/>
Add following line in your spring-servlet.xml
<resources mapping="/images/**" location="/images/" />
and on jsp
<link rel="icon"
href="http://<hostname>/projectname/images/imageName"/>
Try adding the following resources declaration to your Spring configuration:
<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
<resources mapping="/images/**" location="/images/" />
Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.
Your configuration would then look like:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
And your resources would be referenced as follows:
<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
<img src="<c:url value="/resources/images/left_arrow.png" />"

Setting up Tiles with Spring for unprotected resources

I am having problems setting up tiles with a spring mvc in an existing application. Here is my setup info:
All my jsps are under /project/* directory. I am trying to access
JSPs (help files etc.) via static links in other JSPs (like Tiles Test).
I do not have any controllers for these JSPs, and do not want to write controllers for this specific use case (I have tested that it works with controllers), so plz do not suggest writing controllers.
Here are my configurations:
spring-mvc-context.xml entries:
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/spring/tiles-def.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- <property name="prefix" value="/WEB-INF/views/"/> -->
<property name="suffix" value=".jsp"/>
</bean>
tiles-def.xml entry:
<definition name="tilesTest" template="/tilesTest.jsp">
<put-attribute name="title" value="TITLE for TEST-TILES JSP"/>
<put-attribute name="leftMenu" value="MY LEFT MENU"/>
<put-attribute name="body" value="SOME BODY"/>
</definition>
tilesTest.jsp located under /projectWarName/tilesTest.jsp:
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>Tiles Test Page</head>
<title><tiles:getAsString name="title"/></title>
<body>
<tiles:insertAttribute name="body"/>
</body>
</html>
I get the following error when i try to access my jsp (either through a link from another jsp, or directly via the browser). Can someone plz review my configuration and let me know what's wrong?
root cause
java.lang.NullPointerException
org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:42)
org.apache.tiles.template.GetAsStringModel.resolveAttribute(GetAsStringModel.java:178)
org.apache.tiles.template.GetAsStringModel.start(GetAsStringModel.java:95)
org.apache.tiles.jsp.taglib.GetAsStringTag.doTag(GetAsStringTag.java:306)
org.apache.jsp.tilesTest_jsp._jspx_meth_tiles_005fgetAsString_005f0(tilesTest_jsp.java:128)
org.apache.jsp.tilesTest_jsp._jspService(tilesTest_jsp.java:82)
Thanks

Paths in an Intellij Spring-project

So I am currently working on a Spring-project in Intellij. In my JSP-view I have both JQuery, Bootstrap and some local css/js-files.
When I work on the pure HTML in WEBSTORM the paths are easy to interpreter as they just are relative. As far as I understand the paths in Spring are defined in a XML-file and everything goes from there.
My mvc-dispatcher-servlet.xml has this:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
And my files are organized like this:
WEB-INF
css
bootstrap.css
login.css
js
All js files.js
pages
login.jsp
menu.jsp
How do i find the path? Have google like a hero and tried a lot but the files are not found Are there other files that rules over the path-hierarchy?:(:(
Example:
<script src="relative-path/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="relative-path/css/login.css">
Assuming you are using maven:
src/main
java
resources
webapp
static
css
js
...
Then the config..
<mvc:resources mapping="/static/**" location="/static/"/>
then you can use /css/main.css, something like this to access your resources in jsp
But there are different options out there, I will suggest you to read the document

Resources