struts2 jsp file unable to load javascript - struts2

I have a struts 2 application. Version 2.2.3. My problem is that the application is not able to load the javascript files. I have a jsp file that has the following at the top
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Ajax</title>
<script type="text/javascript" src=" <s:url value="/static/script/jquery/jquery-1.6.min.js" includeParams="false"/>" ></script>
<script type="text/javascript" src=" <s:url value="/static/script/jquery/jquery-ui-1.8.12.custom.min.js" includeParams="false"/>" ></script>
</head>
my files are located in the following dir
struts2tutorial/static/script/jquery
In firebug i see the following url
<script src=" /struts2tutorial/static/script/jquery/jquery-1.6.min.js" type="text/javascript">
If i do the following on browser. I get a 404 file not found. Not sure what is that I am doing wrong
http://localhost:8010/struts2tutorial/static/script/jquery/jquery-1.6.min.js

Unless you have specially configured your action extension to match everything, you shouldn't need the /static or the <s:url/> stuff at all. Assuming that the script directory is in your WAR file/exploded directory, this is sufficient:
<script type="text/javascript" src="${pageContext.request.contextPath}/script/jquery/jquery-1.6.min.js"></script>

Putting the javascript files in struts2tutorial/static/script/jquery doesn't necessarily mean that's the path that's used when you deploy the application to your server. Check to see where that path ends up getting mapped to on your server.

try
http://localhost:8010/static/script/jquery/jquery-1.6.min.js
in address bar and the code below in your jsp
<script type="text/javascript" src=" <s:url value="static/script/jquery/jquery-1.6.min.js" includeParams="false"/>" ></script>

For me the problem was in my web.xml.
you have to add " /struts/* " to your filter pattern because the js files aren't really files. I assume this is a struts action behind the url even if it's not detected by the struts2-config-brower plugin.
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<url-pattern>/struts/*</url-pattern>
</filter-mapping>

Related

Internel URL for .ear file deployment [duplicate]

To working my static file (CSS, JS) I have to write absolute path like /AppName/templates/style/main.css. Is there any solution, that I could write relative path like style/main.css?
If your actual concern is the dynamicness of the webapp context (the "AppName" part), then just retrieve it dynamically by HttpServletRequest#getContextPath().
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath}/templates/style/main.css" />
<script src="${pageContext.request.contextPath}/templates/js/main.js"></script>
<script>var base = "${pageContext.request.contextPath}";</script>
</head>
<body>
link
</body>
If you want to set a base path for all relative links so that you don't need to repeat ${pageContext.request.contextPath} in every relative link, use the <base> tag. Here's an example with help of JSTL functions.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<head>
<c:set var="url">${pageContext.request.requestURL}</c:set>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(pageContext.request.requestURI))}${pageContext.request.contextPath}/" />
<link rel="stylesheet" href="templates/style/main.css" />
<script src="templates/js/main.js"></script>
<script>var base = document.getElementsByTagName("base")[0].href;</script>
</head>
<body>
link
</body>
This way every relative link (i.e. not starting with / or a scheme) will become relative to the <base>.
This is by the way not specifically related to Tomcat in any way. It's just related to HTTP/HTML basics. You would have the same problem in every other webserver.
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
Is it recommended to use the <base> html tag?
Just use <c:url>-tag with an application context relative path.
When the value parameter starts with an /, then the tag will treat it as an application relative url, and will add the application-name to the url.
Example:
jsp:
<c:url value="/templates/style/main.css" var="mainCssUrl" />`
<link rel="stylesheet" href="${mainCssUrl}" />
...
<c:url value="/home" var="homeUrl" />`
home link
will become this html, with an domain relative url:
<link rel="stylesheet" href="/AppName/templates/style/main.css" />
...
home link
You start tomcat from some directory - which is the $cwd for tomcat. You can specify any path relative to this $cwd.
suppose you have
home
- tomcat
|_bin
- cssStore
|_file.css
And suppose you start tomcat from ~/tomcat, using the command "bin/startup.sh".
~/tomcat becomes the home directory ($cwd) for tomcat
You can access "../cssStore/file.css" from class files in your servlet now
Hope that helps, - M.S.
Instead using entire link we can make as below (solution concerns jsp files)
With JSTL we can make it like:
To link resource like css, js:
<link rel="stylesheet" href="${pageContext.request.contextPath}/style/sample.css" />
<script src="${pageContext.request.contextPath}/js/sample.js"></script>
To simply make a link:
<a id=".." class=".." href="${pageContext.request.contextPath}/jsp/sample.jsp">....</a>
It's worth to get familiar with tags
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
There is also jsp method to do it like below, but better way like above:
<link rel="stylesheet" href="<%=request.getContextPath()%>/style/sample.css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/js/sample.js"></script>
To simply make a link:
<a id=".." class=".." href="<%=request.getContextPath()%>/jsp/sample.jsp">....</a>
This could be done simpler:
<base href="${pageContext.request.contextPath}/"/>
All URL will be formed without unnecessary domain:port but with application context.
This is a derivative of #Ralph suggestion that I've been using. Add the c:url to the top of your JSP.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:url value="/" var="root" />
Then just reference the root variable in your page:
<link rel="stylesheet" href="${root}templates/style/main.css">

Why do I have to mess with #Script.Render to include scripts in HTML document

It's been a nightmare to me before I came to know that in order to get jquery ui working in ASP.NET MVC I need to add #Scripts.Render("~/bundles/jqueryui"). Before doing so I kept getting Uncaught error: Undefined is not a function. What I did not understand was why on earth this would happen when I could see the jquery ui file in the sources when inspecting the html source. This is the _Layout.cshtml file:
<!DOCTYPE html>
<html>
<head>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.plugins.js"></script>
<script src="~/Scripts/Helpers.js"></script>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>#ViewBag.Title</title>
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
</head>
<body>
#RenderBody()
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryui")//Added later to get it working
#RenderSection("scripts", required: false)
</body>
</html>
In my Helper.js file I have some helper functions that I usually use. One of them is applyDatetimePickerAndFormat that is called on $(document).ready(). Inside that function I have the following code:
$('.txt-date').datepicker({
showAnim: "drop",
changeMonth: true,
changeYear: true,
dateFormat: "dd.mm.yy"
});
If I omit #Scripts.Render("~/bundles/jqueryui") in the _Layout.cshtml I will get the aforementioned error. This code works perfectly with any plain html or web form. So it seems that somehow the document can't see the contents of the jquery-ui file. To make my question concrete:
When I look at the Sources of the the web page I can see jquery-ui-1.8.24.js and it's referenced in the html source. Then why can't the code find jquery-ui functions?
If every java script file has to be specified in the #Scripts.Render then why isn't there any problem with my Helper.js file?
And finally where does this ~/bundles/jqueryui path refer to?
jquery-ui depends on jquery (i.e. it must be defined after jquery) but you have duplicated your files. In the head you have included <script src="~/Scripts/jquery-1.8.2.js"></script> followed by jquery-ui. You then reload jquery at the end of the file using #Scripts.Render("~/bundles/jquery") (Its now after jquery-ui).
Delete the script in the head and it should work. I addition, I recommend you delete jquery.validate and jquery.validate.unobtrusive from the head and use #Scripts.Render("~/bundles/jqueryval") at the end of the file (before #RenderSection..). You can examine these bundles in App_Start\BundleConfig.cs file. There are numerous advantages to using bundles (see Bundling and Minification).
If you are using all these files in every page based on _Layout, you can define your own bundle to includes all files.
You need to define the strategy for your js. I recomend you ot organize your js first and after that separate it to smaller parts. One should be common for all the pages(jQuery in your case) and other scripts for validation should be included only on pages that have some editing fileds etc.
Use DRY principle and read some information about how js works. It helps me a lot some time ago and won't take a lot of time.

loading a web component from a lib subdirectory

When trying to insert a html file for a webcomponent into the views directory in my application library, the web_ui compiler throws the message
FileIOException: Cannot retrieve full path for file 'lib\src\view\entity\base\packages' (OS Error: The system cannot find the file specified.
Is this to be expected? If not, what should I do to fix the error?
Since I don't see your code or project, this is just a hunch. I believe your web component HTML file imports some components the wrong way.
Try the follow this structure:
/lib/src/view/foo/foo.html
/lib/src/view/foo/foo.dart
/lib/src/view/bar/bar.html
/lib/src/view/bar/bar.dart
And inside foo.html:
<!DOCTYPE html>
<html>
<head>
<link rel="components" href="../bar/bar.html" />
</head>
<body>
<element name="x-foo" constructor="FooComponent" extends="div">
<template>
...
</template>
<script type="application/dart" src="foo.dart"></script>
</element>
</body>
</html>
Note that I'm not using packages in foo.html.

Spring mvc jquery ui "accordion is not a function"

I have and accordion working on a normal html page, but when I try and add it to a Spring MVC page then I get the error: $(...).accordion is not a function.
Here is the header of my WEB-INF\views\index.jspfile:
<!DOCTYPE html>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Jeeni Software Ltd</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="<c:url value="css/style.css"/>" type="text/css">
<link rel="stylesheet" href="<c:url value="css/menu/menu_style.css"/>" type="text/css"/>
<script type="text/javascript" src="<c:url value="js/jquery-1.8.3.js"/>"></script>
<script type="text/javascript" src="<c:url value="js/jquery-ui-1.9.2.js">"></script>
<script type="text/javascript" src="<c:url value="js/jquery.flip.js"/>"></script>
...
<!-- This is the 'accordion' div -->
<div id="accordion" style="height:800px;">
<h3>Title...</h3>
...
Here is the ready function:
$(document).ready(function() {
var object = $("#accordion");
alert("object: " + object.accordion);
$("#accordion").accordion({ collapsible: true, active: 'false', autoHeight: false });
$("#banner").fadeIn(1000);
alert("Done");
});
with this alert("object: " + object.accordion); show object: undefined and it bombs out on the next line.
Here is my servlet-context.xml file
<mvc:resources mapping="/cv/**" location="/cv/" />
<mvc:resources mapping="/wow/**" location="/wow/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/imgs/**" location="/imgs/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/inc/**" location="/inc/" />
<mvc:resources mapping="/article-imgs/**" location="/article-imgs/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
Firebug shows no problem loading any of the jquery files. I can re-create the proper behavior outside of Spring mvc, so the problem must be something to do with Spring/Java?!? But I can not find out what. The only error Firebug reports is the $(...).accordion is not a function, but everything is setup just like on the jquery ui accordion example.
BTW, I've trawled the internet for the last 3 hours and found similar problems that were fixed by typos. This isn't a typo error. I can not find anything like this regarding jquery UI and Spring MVC.
Solved!
The problem was that I was importing some content via:
<jsp:include page="../includes/index_page_intro_txt.jsp" />
and this JSP file was re-importing jquery. Took this out and everything was fine.
Special thanks to #NimChimpsky for being a sounding board and keeping me going.
There's no way anyone could have solved this with the information above. So just let you all know how I solved it:
I started by removing everything from the file other than the library imports, the ready statement and the accordion div block. Then it worked. So I slowly added everything back until it stopped working and then investigated that cause.
Should have done that in the first place - duh!
<script type="text/javascript" src="<c:url value="js/jquery-ui-1.9.2.js">"></script>
is not correct in some way.
Can you get any jquery functions to work ?
Is it just the jquery-ui functions that are not working?
Did you customize the jquery-ui download and not include accordion ?
Do you get 404 in the browser debugger ?
Are you opening a popup which overwrites the jQuery object with something else ?
Also probably better to use a cdn hosted version of the jQuery lib files. The below gets the latest version and works with accordion.
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js' type='text/javascript'></script>

Spring Roo , jQuery, jQueryMobile

I am attempting to use jQuery Mobile in Spring Roo.
When I login to my application the application displays the source code for the last included javascript file.
Take a look at mjquery-scripts.tagx file below.
Notice that whatever is the last included file, the source code of that file, will be displayed in the browser.
The address will be something like https://testdomain.com:8080/scripts/jquerymobile/jquery.mobile-1.0.min.js
Now, when I type the url https://testdomain.com:8080/ I will be shown the proper page.
The problem seems to be that on login the redirection is to the path scripts/jquerymobile/jquery.mobile-1.0.min.js .
What could be causing this problem?
Even though I have indicated jquery.mobile this problem happens for jquery.min too.
The default load-scripts.tagx works just fine.
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<spring:url value="/scripts/jquery-1.7.min.js" var="jQuery_URL" />
<spring:url value="/scripts/jquerymobile/jquery.mobile-1.0.min.css" var="mobile_CSS" />
<spring:url value="/scripts/jquerymobile/jquery.mobile-1.0.min.js" var="mobile_js" />
<link rel="stylesheet" type="text/css" href="${mobile_CSS}"><!-- required for FF3 and Opera --></link>
<script type="text/javascript" src="${jQuery_URL}"><!-- required for FF3 and Opera --></script>
<script type="text/javascript" src="${mobile_js}"><!-- required for FF3 and Opera --></script>
This turned out to be a silly problem on my part.
I placed the jquery js files in the /src/main/webapp/scripts folder.
Moved them over to
/src/main/resources/web-resources
as in the following:
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
<spring:url value="/resources/jquery-1.7-min/jquery-1.7.min.js" var="jQuery_URL" />
<spring:url value="/resources/jquerymobile/jquery.mobile-1.0.min.css" var="mobile_CSS" />
<spring:url value="/resources/jquerymobile/jquery.mobile-1.0.min.js" var="mobile_js" />
<link rel="stylesheet" type="text/css" href="${mobile_CSS}"><!-- required for FF3 and Opera --></link>
<script type="text/javascript" src="${jQuery_URL}"><!-- required for FF3 and Opera --></script>
<script type="text/javascript" src="${mobile_js}"><!-- required for FF3 and Opera --></script>
</jsp:root>
Now it works as expected.

Resources