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
Related
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
I have the following folder structure. I added mvc annotation and resources path but when I try call the resources in home.jsp like <img src= "/resources/images/spitter_avatar.png" />, it can't find anything.
Folder structure:
Here is my code in my servlet-config.xml file for resources:
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<img src= "resources/images/spitter_avatar.png" />
try above remove "/" before resources
if you wanted to start with / , then use JSTL C tag
<img src= "<c:url value="/resources/images/spitter_avatar.png"></c:url>" />
The resources folder must be located inside the WebContent folder.
Place ${pageContext.request.contextPath} before your items url on the jsp page. Example:
${pageContext.request.contextPath}/resources/images/springlogo.svg
I am working on Spring MVC 3.x. Can we use properties files contents in HTML/JavaScript file?
I have a properties file at the classpath named webMessages.properties.
And Spring -servlet.xml has
<context:property-placeholder location="classpath:/jdbc.properties,classpath:/webMessages.properties" />
When I try to access the properties in the JavaScript file, it is not giving any output:
<p>"${topPath.topHeading}"</p>
Kindly advise, if this is possible?
I am not sure if context:property-placeholder will expose properties outside the definitions. But I was able to get it by the following way:
<bean id="propertyPlaceHolderConfigurer" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
<value>classpath:/webMessages.properties</value>
</list>
</property>
</bean>
<context:property-placeholder
properties-ref="propertyPlaceHolderConfigurer"
ignore-resource-not-found="true"/>
In your jsp:
Include this tag:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
and then access the property as:
<spring:eval expression="#propertyPlaceHolderConfigurer.getProperty('topPath.topHeading')" />
What you are trying to do is possible, but the file needs to be a JSP. Inside the JSP try the following:
${properties['topPath.topHeading']}
I'm using spring with a VelocityViewResolver configured by Spring
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="1" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/velocityToolbox.xml" />
</bean>
This works if the velocityToolbox.xml file is under WEB-INF ; however I'm trying to put it in the classpath. Is there a way to do it ?
I've tried specifying toolboxConfigLocation as "classpath:/wherever/velocityToolbox.xml", but it does not find the resource, and I end up with no toolbox configured, and a NPE at runtime (for some reason, it seems like the code expect the location to start with a '/', or add the '/' itself before looking for the resource).
Given that the resource is located using ServletContext.getResourceAsStream, with the content of the toolboxConfigLocation property prefixed by a "/", is there a way I can "define" a resource in my spring config that would somehow 'point' to an actuall classpath resource ?
Any idea is welcome.
I believe it has to be a full path, eg:
/WEB-INF/classes/com/myapp/resources/toolbox.xml
The ServletToolboxManager#getInstance(ServletContext context, String toolboxConfigLocation) method is used to generate a ToolboxManager configured with the toolbox in order to create the ChainedContext used in the VelocityToolboxView.
This method pre-appends the path with a '/' if one does not exist, and then uses ServletContext#getResourceAsStream(String path) to read it in.
With this in mind, you'll have success if you set it as a full path from the context root.
i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/> -->
<property name="locations" value ="config/springcontext.properties" />
The outcommented one is working and the other one i dont get to work :/ Can someone help?
Edit:
Thx 4 comments so far.
Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?
You can try something like this:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/servlet.properties" />
And define ext.properties.dir property in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:
-Dext.properties.dir=file:/usr/local/etc/
BTW, very useful blog post.
You can use file prefix to load the external application context file some thing like this
<context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
<context:property-placeholder location="classpath*:spring/*.properties" />
If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above
<property name="locations" value ="config/springcontext.properties" />
this will be pointing to web-inf/classes/config/springcontext.properties
This blog can help you. The trick is to use SpEL (spring expression language) to read the system properties like user.home, to read user home directory using SpEL you could use #{ systemProperties['user.home']} expression inside your bean elements. For example to access your properties file stored in your home directory you could use the following in your PropertyPlaceholderConfigurer, it worked for me.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value>
</property>
</bean>
This question is kind of old, but wanted to share something which worked for me. Hope it will be useful for people who are searching for some information accessing properties in an external location.
This is what has worked for me.
Property file contents:
PROVIDER_URL=t3://localhost:8003,localhost:8004
applicationContext.xml file contents: (Spring 3.2.3)
Note: ${user.home} is a system property from OS.
<context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/>
<bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">${PROVIDER_URL}</prop>
</props>
</property>
</bean>
${PROVIDER_URL} got replaced with the value in the properties the file
One way to do it is to add your external config folder to the classpath of the java process. That's how I've often done it in the past.
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />
This works for me.
Local development machine path is C:\apps\tomcat\ath\ath_conf and in server /apps/tomcat/ath/ath_conf
Both works for me