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

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"%>

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!

Import extJs to JSP

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>

Liferay translation not working with selection break

Hi I'm using liferay version 6.1.2 ce GA3.
I noticed that when i create my own structures and they are containing selection breaks the content within is not translatable.
Every content in a selection break can be marked as "localizable" but even if I do that when I try to add a translation the pop-up is empty with just the title of the web-content.
To me this problem occurs since I upgraded from 6.1.1 CE GA2 but I noticed that even with a new clean installation it's the same.
Hope somebody can help me here.
added Ticket on Issuetracker enter link description here
Internationalization(i18n) in liferay 6.1
Complete working program
My project name is AUIproj.
In Language.properties which labels or the data we want to translate then we have to write inside this file. Suppose in my AUIproj I just want to change the name of my labels and my title only so we have to follow these steps:
Step 1:
First create a folder inside docroot/WEB-INF/src by the name of content, inside content folder, create a file Language.properties.
Step 2:
copy this content to Language.properties
javax.portlet.title=AUI Portlet
product-name=Product Name
product-serial=Product Serial
Step 3:
right click on project then build language lots of supported language files will create inside your content folder automatically and all files contain the same contents that present in your
Language.properties files.
Step 4:
Now we have to just write that specific labels name on that specific file and change the contents on that place.
Use google translator and paste them suppose for hindi I just wrote in hindi file like
javax.portlet.title=भाषा
product-name=उत्पाद नाम
product-serial=उत्पाद धारावाहिक
The name of hindi Language file is Language_hi_IN.properties.
In init.jsp I declared lots of taglib, lots of java classes by import statements.
init.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%# taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%# taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%# taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%# taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%# taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%# page import="java.util.List" %>
<%# page import="java.util.Calendar" %>
<%# page import="java.util.Collections" %>
<%# page import="com.liferay.portal.kernel.util.HtmlUtil" %>
<%# page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%# page import="com.liferay.portal.kernel.util.CalendarFactoryUtil" %>
<%# page import="com.liferay.portal.kernel.dao.search.ResultRow" %>
<%# page import="com.liferay.portal.kernel.dao.search.SearchEntry" %>
<%# page import="com.liferay.portal.kernel.exception.SystemException" %>
<%# page import="com.liferay.portal.kernel.util.WebKeys" %>
<%# page import="com.liferay.portal.security.permission.ActionKeys" %>
<%# page import="com.liferay.portal.kernel.util.ListUtil" %>
<%# page import="com.liferay.portal.service.permission.PortalPermissionUtil" %>
<%# page import="com.liferay.portal.service.permission.PortletPermissionUtil" %>
<%# page import="javax.portlet.PortletURL" %>
<liferay-theme:defineObjects />
In view.jsp just imported init.jsp
view.jsp
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%# include file="init.jsp" %>
<portlet:defineObjects />
This is the <b>AUI Portlet</b> portlet in View mode.
<portlet:actionURL name="addProduct" var="addProductURL" />
<aui:form action="<%= addProductURL.toString() %>" method="post">
<aui:fieldset>
<aui:input name="productName" />
<aui:input name="productSerial" />
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
</aui:fieldset>
</aui:form>
build.xml
<?xml version="1.0"?>
<!DOCTYPE project>
<project name="AUIproj-portlet" basedir="." default="deploy">
<import file="../build-common-portlet.xml" />
<target name="build-lang">
<antcall target="build-lang-cmd">
<param name="lang.dir" value="docroot/WEB-INF/src/content" />
<param name="lang.file" value="Language" />
</antcall>
</target>
</project>
Paste this line just in portlet.xml just after tag
<resource-bundle>content.Language</resource-bundle>
Finally at first time build language and if you change any data on properties files then build language else no need to build language again and again. But some time it is showing some warnings like in future …...properties file will modified so to again build language and this time the contents of hindi properties file will not change.
Finally go to control panel of liferay -> Test test -> my account -> miscellaneous-> display settings -> Language select hindi and you can see the the title and our labels will change into Hindi language. If you face any problem then please share with me
Thanks and regards
asif aftab

Tiles 2.2 put-list-attribute + Spring 3.1.4

I am migrating our application from struts 1.3 to spring 3.1.4 mvc. In the process i am also upgrading from tiles 1.1 to tiles 2.2. Jstl version is 1.1. With Tiles 2.2, i am having an issue about using put-list-attribute. I am trying to show a simple jsp page which should does following:
a) Header : include bunch of css (i commented it out as it doesn't work. Using firebug i see error "NetworkError: 404 Not Found - http://localhost:8080/appname/%BeachStyle%7d" --- {eachStyle}
No idea where %BeachStyle% came from.
b) Header : include title -- Works fine
c) body : show static text
tiles.xml
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name=".login" preparer="com.tiles.LoginController"
template="/tiles/layouts/layoutmain.jsp">
<put-attribute name="pageTitle" value="vivi test" />
<put-list-attribute name="baseStylesTest">
<add-attribute value="/styles/css/grids.css"/>
<add-attribute value="/styles/css/superfish.css"/>
<add-attribute value="/styles/css/styles.css"/>
</put-list-attribute>
</definition>
</tiles-definitions>
layoutmain.jsp
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<tiles:useAttribute id="stylesList" name="baseStylesTest" classname="java.util.List"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%-- <c:forEach var="eachStyle" items="${baseStylesTest}">
<link type="text/css" rel="stylesheet" href="<c:out value='${eachStyle}'/>" /> --%>
<title><tiles:insertAttribute name="pageTitle" /> </title>
</head>
<body>
Login page body
</body>
</html>
I tried the following:
i) Checked the tiles2.2 dtd and example to use it - found similar usage
ii) Checked examples over internet and how it's being used - found similar usage
iii) Checked how its used with struts -- found they use #attr.xxx as it is in pagescope
I don't see anything wrong in my implementation. The title attribute is rendered properly but on the attribute belonging to put-list-attribute.
Any help is much appreciated.
Found the solution. JSTL wasn't working as i declared my web-app version to be 2.5. This is not compatible with tomcat5.5 which i was using. Changed the web-app version to be 2.4 and all is good.
<?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_4.xsd">
---
</web-app>

Internationalization sitemesh

I'm using freemarker, SiteMesh and Spring framework.
For the pages I use ${requestContext.getMessage()} to get the message from message.properties. But for the decorators this doesn't work. How should I do to get the internationalization working for sitemesh?
You have to use the fmt taglib.
First, add the taglib for sitemesh and fmt on the fisrt line of the decorator.
<%# taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%# taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<fmt:setBundle basename="messages" />
In my example, the i18n file is messages.properties. Then you need to use the fmt tag to use the mesages.
<fmt:message key="key_of_message" />
If you prefer templates and the freemarker servlet instead you can enter the following in your templates:
<#assign fmt=JspTaglibs["http://java.sun.com/jstl/fmt"]>
<#fmt.message key="webapp.name" />
and in your web.xml:
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>messages</param-value>
</context-param>

Resources