Eclipse and struts syntax errors in JSP files - java

I'm using eclipse as my java IDE and the project I'm working on uses struts. Eclipse is telling me on any page that has struts tags that the tags are syntax errors.
E.g. my tag <logic:equal shows this error
Syntax error on token "<", delete this token
but the page works just fine. How can I get eclipse to not show errors in these cases?
EDIT: Just noticed this is only when the tag is inside a <script> block. Tags in regular HTML work fine. Is this just an unresolved bug with my version of eclipse?
EDIT 2: posting a code block per the comment. Also added the CDATA to the file like the response suggests. I'm still getting the error in eclipse.
<script type="text/javascript" language="Javascript1.2">
//<![CDATA[
window.onload = function ()
{
<logic:equal value="false" name="BeanKey" property="value(RecordNotFound)">
alert("Record not found");
</logic:equal>
}
//]]>
</script>

Instead you can try like this,
<logic:present name="BeanKey" property="value(RecordNotFound)">
<logic:equal value="false" name="BeanKey" property="value(RecordNotFound)">
<script>
window.onload = function ()
{
alert("Record not found");
}
</script>
</logic:equal>
</logic:present>

Enclose your JavaScript into a CDATA block:
<script type="text/javascript">
<![CDATA[
var javascript;
]]>
<script>

Related

Jquery Load function not referring master page script files

I am using jquery load function in my application's masterpage.html and it is working fine. But the problem is i need to put Jquery.min.js in all my content pages in order to use jquery functionality in my content page. I don't want to include it in all pages. It should refer from my master page.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
<script type="text/javascript">
function loadContent(pagename)
{
$("#output").load(pagename);
}
</script>
</head>
<body>
About
<div id="output"></div>
</body>
</html>
about.html
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
<div id="content">
This is about page !!
</div>
As you see my about.html again am including jquery in order to use it in my about.html page. How to avoid this in all pages ? Please help
No, it is not possible . You have to include it in all pages where you are using jquery
Instead of referring to the url like below ,
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
You can download the jquery library to your project path and use it like
<script src="project_name/folder_name/jquery.min.js">
But it is compulsory to include jquery in all pages
Hope this helps !!
I can't give you a best answer, these is my advice
the jQuery file had better include at the bottom in body tag.
If you use the jQuery,you must include it.

Calling Struts2 Namespace from Normal HTML form

Exception :
Description: The requested resource /Strut2Examples/checkMethods/updateCRUD is not available. How to call the namespace based action from normal HTML for with Struts2. It works with Struts2 Forms. Please help me to understand.
HTML :
<s:form namespace="/checkMethods" action="executeCRUD" >
<s:submit label="execute" value="execute" />
</s:form>
<form name="normalForm" id="normalForm">
<input type="button" value="update" onclick="submitForm()"/>
</form>
Java Script :
<script type="text/javascript">
function submitForm()
{
var myForm = document.getElementById("normalForm");
myForm.action="checkMethods/updateCRUD";
myForm.submit();
}
</script>
If you want to use HTML form tag you should build the URL using the action name and namespace attributes of the url tag. For example
<form action="<s:url namespace="/checkMethods" action="deleteCRUD"/>" method="POST">
The same concern the JavaScript code where you could mix url tag. Like this
myForm.action='<s:url namespace="/checkMethods" action="deleteCRUD"/>';
Actually if you define the action attribute of the form you don't need to construct the URL in the event handler function. Just do submit() .
You should take url tag seriously, because for example you have constructed URL in the action attribute that lacks context path and slash and errors like that you did can't count.

struts 1.0 logic tag choice - if/else logic

What I have is a dropdown list. When the user selects a certain option, where each option represents a specific String on the Java server side.
Right now, the Java server side able to check which option is selected, and the number to correspond. At the moment, I am able to output the value in Java backend, not on the JSP page.
Is there an if/else tag for Struts 1.0?
I am not sure which logic tag is the best to pass a Java value for frontend processing:
JSP page
if(value = 666)
this textbox is readonly
else
this textbox row is active
My research so far:
Looking at logic:equal, it seems to pass a value on the JSP page using taglibs like below. This doesn't work for me, because I want to pass the value FROM a Java class on the server side.
<logic:equal name="name" property="name" value="<%= theNumber %>" >
<c:choose>
<c:when test="${the number}">
Both are equal.
</c:when>
<c:otherwise>
Both are not equal.
</c:otherwise>
</c:choose>
this is jstl tag
you need to use
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
The JSTL answer is the best one, however, in my case it was an old Java legacy application without JSTL and I couldn't introduce it.
This was the requirement I had with some legacy Struts 1.3 code. Set a checkbox to match the value of a form bean's value from a database call, as in "Y", "N", "", or null.
I also had to keep in within struts logic tags and not use JSTL, which was my original preference. I know that the struts docs
said the checkbox should equate to a boolean variable in the
actionForm, but I used a string value, which worked.
From the docs link above:
NOTE: The underlying property value associated with this field should be of type boolean, and any value you specify should correspond to one of the Strings that indicate a true value ("true", "yes", or "on"). If you wish to utilize a set of related String values, consider using the multibox tag.
I had thought about converting my string values to booleans in the
form, but it took the strings this way.
Last of all, I used some javascript to set the checked status. All of this is verbose and not the best solution, but faced with the requirement, it does work.
Here is the code:
<logic:equal name="<%=formName%>" property="formInd" value="Y">
<html:checkbox name="<%=formName%>" onchange="setDataChanged()" property="formInd"/>
<script type="text/javascript" LANGUAGE="JavaScript">
document.<%=formName%>.formInd.checked = true;
</script>
</logic:equal>
<logic:equal name="<%=formName%>" property="formInd" value="N">
<html:checkbox name="<%=formName%>" onchange="setDataChanged()" property="formInd"/>
<script type="text/javascript" LANGUAGE="JavaScript">
document.<%=formName%>.formInd.checked = false;
</script>
</logic:equal>
<logic:empty name="<%=formName%>" property="formInd" >
<html:checkbox name="<%=formName%>" onchange="setDataChanged()" property="formInd"/>
<script type="text/javascript" LANGUAGE="JavaScript">
document.<%=formName%>.formInd.checked = false;
</script>
</logic:empty>

JSTL for each, var contains square brackets

I have a HashSet of Strings, which is made with the following code:
Set<String> scripts = new HashSet<>();
String contextPath = request.getContextPath();
scripts.add(contextPath + "/resources/scripts/jquery.cycle2.js");
scripts.add(contextPath + "/resources/scripts/jquery.cycle2.center.js");
scripts.add(contextPath + "/resources/scripts/slideshow.js");
request.setAttribute("scripts", scripts);
Now in a JSP page, with JSTL, I do a normal forEach loop:
<c:if test="${not empty scripts}">
<c:forEach var="script" items="${scripts}" >
<script type="text/javascript"
src="${script}">
</script>
</c:forEach>
</c:if>
When loading the page, this results in:
<script type="text/javascript"
src="[/InfoKiosk/resources/scripts/jquery.cycle2.center.js">
</script>
<script type="text/javascript"
src=" /InfoKiosk/resources/scripts/jquery.cycle2.js">
</script>
<script type="text/javascript"
src=" /InfoKiosk/resources/scripts/slideshow.js]">
</script>
Notice the square brackets ([ and ]) that appear before the first script source and after the last. Where do they come from?
For some reason it is calling toString() on your set. This then turns your set into [script1, script2, script3], calling foreach on this string splits on the comma, creating the effect we see.
I could see exactly what you were seeing when I replace
request.setAttribute("scripts", scripts);
with
request.setAttribute("scripts", scripts.toString());
I could not reproduce what you were seeing without this, however I was running java 6.
Not an answer, but a helpful insight I hope!
The problem occured because the scripts variable was set in a JSP through an attribute for a custom tag, like this:
<t:genericpage scripts="${scripts}">
....
Of course, this converted the collection to a string by calling its toString() method. We have solved it in a different way, by setting the request attribute in the servlet.

Problem with jquery and IE 8 and later

In Firefox work, but when load page in IE8 i get only blank page and source HTML-code available.
I delete
<script src="/NewsManager/js/jquery.js"
type = "text/javascript"
</script>
and all work without script.
NetBeans 6.7, ApacheTomcat, JSP
You missed the closing > in second line:
<script src="/NewsManager/js/jquery.js"
type = "text/javascript"> <!-- close the opening script tag HERE -->
Try this:
<script src="/NewsManager/js/jquery.js" type="text/javascript"></script>

Categories