Struts2 parameter to javascript - java

I have a struts2 action with a field: private Long omradeId;
The field has a getter.
The action is sent to a jsp and within that jsp i can access the field using <s:property>tag. Thats all good.
Now i also have within the jsp a section where i define a <script>. Within that script i would like to create a variable that will build a url with the above mentioned struts2 field as a value.
<script type="text/javascript">
var url = "/path/to/action?parameter1=";
</script>
How can i put the value of omradeId after the equals (=) sign? I tried using <s:property>but that did not work.
Any suggestions?

"/path/to" will change depending on the web server. To avoid this use the struts2 url tag.
See: http://struts.apache.org/2.x/docs/url.html
For an action called "action" in namespace "/" with a parameter called parameter1 having the value omradeId, you would simply say:
<s:url namespace="/" action="action">
<param name="parameter1" value="%{omradeId}"/>
</s:url>
putting the above into the JS variable we have:
var url = "<s:url action="action"><param name="parameter1" value="%{omradeId}"/></s:url>";
Using the above will mean your application can be installed on different application servers without change.
Having formated xml is nicer than inline, if using a lot of parameters adding the var parameter to the s:url tag to give it a name and then you can reference this string in a number of places with the s:property tag would keep things clean.
<s:url namespace="/" action="action" var="myString">
<param name="parameter1" value="%{omradeId}"/>
</s:url>
var url = "<s:property value="#myString"/>";

This should work:
<script type="text/javascript">
var url = "/path/to/action?parameter1=<s:property value="omradeId">";
</script>
If not you should check if the value is not null and value is successfully set in your action class.

Related

Can't concatenate Javascript and Struts tag

I'm trying to create invisible divs with jQuery which in title will be populated dinamically by an struts tag, but its giving me invalid indexed property error:
Invalid indexed property 'niveisRisco['+<%=ctr%>+']
Heres the code i'm using:
$(function(){
var mapaAplicacaoJs = $('#MapaAplicacao area');
var mapaPerfilJs = $('#MapaPerfil area');
<logic:present name="carteiraSelecionada">
<logic:iterate name="carteiraSelecionada" property="carteiraAtual.niveisRisco" id="foo" indexId="ctr">
mapaAplicacaoJs.each(function() {
mapaAplicacaoJs.before('<div id="nivel_risco_dv" class="tabindex" title="<bean:write name="carteiraSelecionada" property="carteiraAtual.niveisRisco['+<%=ctr%>+'].getDescricao" />"></div>');
</logic:iterate>
</logic:present>
});
If i hardcode <div id="nivel_risco_dv" class="tabindex" title="<bean:write name="carteiraSelecionada" property="carteiraAtual.niveisRisco[0].getDescricao it works.
Can anyone tell me whats wrong with my concatenation?
Thanks
There is not attribute with name indexId in logic:iterate tag. See logic:iterate
Access it with $ctr instead of doing it via scriplet

Parameter passed to JSP via struts to JSP variable

Im a Java newbie, I have this code working which obtains the brandCode parameter and places in a hidden input named "brandCode"
<html:form method="get" action="/catalogindexsearch.do" styleId="sortAndNavigationForm">
<input type="hidden" name="formAction" value="searchDisplay" />
<html:hidden property="brandCode" />
</html:form>
But I wish to use the parameter elsewhere in my JSP i.e.
<h1>Brand Code: ${brandCode}</h1>
So my question is how can I create a Java variable from the html:form URL parameter?
In JSTL you can access a form property by form name
<h1>Brand Code: ${formName.brandCode}</h1>
Give it an ID like this:
<html:hidden property="brandCode" id="brandcode" />
then access the value by:
var brandCode = $("#brandcode").val();
or if you're not using jQuery:
var brandCode = document.getElementById("brandcode").value;
If you're trying to use the value of a property from your action class, you had it right - It's
${brandcode}
where brandcode is a property in your action class with getters and setters:
private String brandcode;
public String getBrandCode() {
}
you get the point.
What I was after in the end was this ${param.brandCode} or this ${param["brandCode"]}.

how to get html struts styleClass with javascript/jquery

I need to get the value of the styleClass element in javascript. Page is in jsp with struts/html tag elements. jsp code is
<input type="hidden" class="filename" name="filename" value="<%= filename %>" />
<html:file property="testfile" styleClass="testfile"/>
and onclick of button I invoke the javascript
function fields() {
var filename = jQuery('.filename');
alert(filename);
var testfile= jQuery('.testfile').val();
alert(testfile);
}
However in the firstcase(filename) I get [object object] returned and in second case I get "undefined". Can someone give some pointers on how to get the uploaded file name in jquery. Thanks
If you want to get a value from an input:
var filename = $('.filename').val();
or
var filename = jQuery('.filename').val(); (same as above)
read more here -> jquery selector can't read from hidden field
Try this
var testfile= jQuery('.testfile').attr('value');
It may be that your browser is preventing access to the value attribute for security reasons. Try both options in different browsers - Chrome, Firefox, IE, etc.

Accepting http session request parameter in jsp page using jquery

I am working in a web application using struts. I am performing some operations in struts action class and using session.setAttribute i am setting result value. My struts action class sends result to jsp page where i am displaying result. I have to display message after loading jsp page, hence i am trying to use jquery. But i am not getting how to handle http sesion request in jquery.
Earlier i was taking one property in sturts-config.xml and setting the value instead of sending http session request parameter. But it doesn't work for me, because every time when i refresh my page it takes initial value from struts-config.xml. i am taking struts property as hidden field in jsp page by writing:
<html:hidden property="propname" styleId="id_propname" name="formname" />
for setting property value null I am writing following code in jquery.
<script type="text/javascript">
$(document).ready(function()
{
alert("some text");
$("#id_propname").val("");
});
</script>
Is there any way where i can directly get the http session request in jquery ? please reply me if you can give some suggestions I thanks to all valuable suggestions in advance.
IMHO there is no simple way to pass your session to the jquery . What you can do is to set the value of some hidden field and declare a global variable on the script and access the value and check for the valid condition if any .
Although its ugly and highly discouraged to use scriptlet inside jsp , this might work :-
<% String sessionVal = session.getAttribute("yourSessionValue").toString() %>
<html type="hidden" id="id_propname" name="propname" value=<%=sessionVal> >
In your jquery :
<script type="text/javascript">
var sessionVal;
$(document).ready(function()
{
sessionVal = $("#id_propname").val();
alert("this is the session value at Script " + sessionVal );
});
</script>
Null check for session is not done.
Or
If you are using tag libraries you can set the session value on form bean and use something like this in jsp :-
<logic:equal name="yourFormBean" property="yourProperty" value="someDefinedValue">
Do your action here
</logic:equal> <br />
Well http Sessions and Requests, in the form of java Objects and related to servlets, live in the server side. JQuery on the other hand will "normally" live in the client side... therefore your problem description is a bit "off".
The parameters you "send" in the httpsession or/and in the httprequest are for the jsp generation where jQuery does not play any part. What then gets sent to the browser is the HTML generated by the jsp engine. This HTML when in the browser can then be used by jQuery.
By the way the framework name is Struts and not Sturts.
Taking in account this information I don't really understand what in fact you want to do.

Spring MVC: Relative URL problems

I have a controller bound the URL: "/ruleManagement".
Inside my JSP, I have a form that forwards (on submit) to "ruleManagement/save" url. When there are errors with the input fields, I want it to return back the original form View. This is where the problem starts...
Problem 1) Now that the URL is "/ruleManagement/save", my form submit now points to "/ruleManagement/ruleManagement/save".
Problem 2) I tried using spring:url tag to generate the absolute paths for me, which usually works great. But when I put a spring:url tag inside of a tag, the spring:url tag does not get parsed correctly.
<form:form action="<spring:url value='/ruleManagement/save' ...>" method="post">
When I analyze the DOM after the page loads, my form tag looks something like:
<form action='<spring:url value="/ruleManagement/save" />' ... >
If I don't use the spring:url tag, and instead use just "/ruleManagement/save", the url generated excludes my application name in the url, which is also wrong.
How do I generate a consistent URL pattern across all Views regardless of path? If the answer is "using spring:url", how do I get that content inside a form:form tag?
Custom tags in JSP can't be used in attributes of other custom tags, so you need to store intermediate result in a request attribute (using var to redirect output of the tag to the request attribute is a common idiom supported by many tags):
<spring:url var = "action" value='/ruleManagement/save' ... />
<form:form action="${action}" method="post">
I too would love to be able to generate a consistent URL path across all Views! Is this possible with <spring:url .../>.
To answer your second question & tacking on to axtavt's answer, embed the <spring:url ... /> into the form action after adding the property htmlEscape="true"
Example: <form:form action="<spring:url value="/ruleManagement/save" htmlEscape="true" .../>" method="post">

Categories

Resources