How to pass a non-string Java object to an ActionBean - Stripes - java

I'm using the Stripes framework. I want to pass non-string Objects to an ActionBean. Is this possible?
I am trying to do:
<s:url var="statementUrl" beanclass="sempedia.action.StatementActionBean" prependContext="false" >
<s:param name="property" value="${row.key}" />
<s:param name="values" value="${row.value}" />
<s:param name="myString" value="Why kick a moo cow" />
</s:url>
<jsp:include page="${statementUrl}"/>
Where row.key resolvs to a custom class I have defined and row.value is an ArrayList of a custom class I have defined

Nope, nothing really.
I mean, there's always a way. You could serialize the forms out to a byte array and Base64 encode in to a string and then pass that as an argument.
But then you start running in to URL limits (they can only be so long).
If practical, you could save the data in the Session and simply refer to it later. You could use Stripes FlashScope, which stuffs it in the Session but only for the next request, then it goes away.
You could encode the data in to a HTML form, but then you would need to POST that rather than use a GET for it.
You could save the data out to another store (a database, memcache, something like that), and simply return a key to it, then pass in the key.
Really depends on the lifecycle of what you're trying to do, and the nature of the data.

Related

How does the Spring forms .jsp tag library work?

So I have a .jsp page which has a form on it, like this (naturally this is a massive simplification):
<form:form commandName="myCommand" method="post">
<form:select path="values" class="select-tall" multiple="multiple" id="mySelect">
<option>first</option>
<option>second</option>
<option>third</option>
</form:select>
<button type="submit" class="button">Save</button>
</form:form>
When the submit button is pressed, the form is submitted(somehow) and the path= attributes are used to bind the data inside the form elements to the properties of an instance of a plain old java object. I understand how this POJO is specified, and I understand how the POST request is routed to the correct controller, but I don't understand where or how the form values are mapped to the given POJO.
What I don't understand is:
How does the spring tag library modify the form such that this binding takes place?
How would one go about doing this in a manual or ad-hoc fashion(using a Javascript onSubmit() method, say)?
Essentially my question is: How does the spring-form.tld tag library work?
Any resources, or even a high-level explanation in your own words, would be extremely helpful.
I've implemented a work-around solution in the mean time (dynamically adding items to a hidden form element), but I feel like this is hack-y and the wrong solution.

Websphere Commerce-TypedProperty

Can anyone help me to understand the usage of TypedProperty in websphere commerce?
ie,How to pass values from one jsp to other using TypedProperty without a command class.I would prefer to handle it in my client side itself without invoking Command class..can anyone help me to sort out it?
Typed property is usually used to pass values from controller commands to JSPs. If you just want to pass values from one JSP to another, create a form in your first JSP and submit it to the second.
If this is a form submit, set the values you need to pass in element. In the results jsp you can get those values using ${WCParam.xxx} .
FYI - To list out all the values in WCParam object try to print the below in JSP :
${WCParamValues}
We use typedProperty when we need to send anything from the command. For example, you give an order ID from the first JSP and want to get the final amount to be passed the result JSP. Here in the command we use the orderID from the request object -> Then we use the OrderAccessBean to get the OrderTotal -> then we set this to a TypedProperty object -> we then set this TypedProperty object to request properties using setRequestProperties() OOB method in a controller command.
Hope this makes it clear !
TypedProperty is a class in Java which can be compared to Hashmap in Java for better understanding. It is a name value pair combination.
I just wanted to understand the problem before answering further.
Why do you want to use TypedProperty in Jsp to pass the value from one jsp to another?
Are you importing the second jsp or including the second jsp to which you have to pass the values to?
If you are importing, you can use c:param tag to pass the values to the second jsp.
For included jsps, the values are already available in the second JSP.
Please include code snippets to explain your problem so that it can be answered clearly.
You can pass parameters from one jsp to another by using the following code snippet:
<c:import url="child.jsp">
<c:param name="name1" value="value1" />
<c:param name="name2" value="value2" />
<c:param name="name3" value="value3" />
</c:import>
Within the child.jsp you can read the parameters by using:
<c:out value="${param.name1}" />
<c:out value="${param.name2}" />
<c:out value="${param.name3}" />
A TypedProperty is nothing but a Wrapper to HashMap. So that's nothing to do here with passing values from one JSP to another JSP. Without invoking a command, you can't pass a Java object to another JSP.
And that is the very basic of Command Framework. I would prefer to go with the first answer.

Receive Linked List from client using Java GET

I'm receiving string from client like following -
String time_S = request.getParameter(Message.KEY_TIME);
Now, If I want to receive a linked list data how should I do that? I tried to use getParameterValues but I don't think I could use it properly.
Thanks in advance.
You can't really retrieve a 'linked list' per se over HTTP - it needs to be serialized (transformed from a Java object to a string). There are plenty of ways of doing this, but you might have them send it to you as a set of comma separated values and then parse it into a linked list or java data structure of your choice.
If you have them send if via JSON, there are several libraries that can be used which will change them to Java standard objects. Such as Simple.JSON, it turns JSON Array's into Java List objects, or JSON Object's into Java Map's.
getParameterValues might be the easiest way, provided that the client can provide each element of the list as a repeated querystring parameter.
For example if the client can send this querystring:
color=red&color=white&color=blue
getParameterValues("color") will return {"red", "white", "blue"}
Depends on how you submit the data. For example, if you submitting data from a web page and you are submitting data using the same parameter name, you can use the getParameterValues method.
For example, take the following inputs:
<input type="text" name="time" />
<input type="text" name="time" />
<input type="text" name="time" />
Then you can access the parameters as follows
String[] times = request.getParameterValues("time");
And if you need that linked list, just do the following
LinkedList<String> timeList = new LinkedList<String>(Arrays.asList(times));

JSF 2.0 AJAX: Call a bean method from javascript with jsf.ajax.request (or some other way)

Some background: I am building a custom JSF component. The component is basically a text editor and it should have a "Save" -button for saving the content string of the editor. As I am using the CodeMirror library, I need to fetch the content (string) from the editor with javascript and send that to the server. Therefore, in this case I cannot use XML-based JS invocation such as f:ajax.
The question: I was planning to send the string with jsf.ajax.request, but it doesn't directly support calling methods on beans. How can I invoke a method in a bean with JSF in AJAX manner?
There at least two ways to get around this:
Include a hidden form to page with hidden inputfield. Update that inputfield from javascript and then call jsf.ajax.request to post that form. Custom actions can be invoced in the property's getter or setter if needed.
Do the request with raw XMLHttpRequest (or maybe with help from some other JS library). Create a servlet and call that.
Both ways are clumsy and the latter also breaks out of JSF scope.
Am I missing something? How do you do these?
There is a quite similar question, but the answers given only refer to XML-based AJAX invocations. There is also another similar question, but that refers to XML-based AJAX calls as well.
I couldn't find out how to call beans direcly with javascript, but here is a hack around calling f:ajax-declaration from javascript:
1) Create a hidden form with fields for all the data that you want to send to the server. Include a h:commandButton as well:
<h:form id="hiddenForm" style="display: none;">
<h:inputHidden id="someData" value="#{someBean.someData}" />
<h:commandButton id="invisibleClickTarget">
<f:ajax execute="#form" listener="#{someBean.myCoolActionOnServer()}" />
</h:commandButton>
</h:form>
As usual, listener attribute, #{someBean.myCoolActionOnServer()} in this case, refers to the method that you want to execute on the server.
2) In some other button use onclick to call for your special javascript AND click the trigger-button via javascript:
<h:commandButton value="Click me" onclick="populateTheForm('hiddenForm'); document.getElementById('hiddenForm:invisibleClickTarget').click(); return false;" />
populateTheForm() should actually fill the data into hiddenForm's fields.
This is a simplification of my case but should work. Still looking for more conventient approach, though.
I did this task several times. Yo don't need multiply hidden fiels. You can use only one hidden field, convert all input values to JSON object via JSON.stringify and set into this field. On the server side - deserialize JSON object (there are many Java libs for that) to an Java class. That's all.

Can I pass complex parameters to a Java Applet?

I'm really new to Java and I have to create an applet for signing documents electronically. The applet will be called from an ASP.Net web page application.
Right now, I embed the applet in page as a <object id="EDOCApplet" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"> and send parameters to the applet like this:
<PARAM id="EdocPath" NAME="EdocPath" value="\\some\where\file.txt" />
In the applet, I can get the value using applet's built-in method getParameter("EdocPath");
What I need is the ability to pass the applet a list of several files and their "display names". For instance, it would be simple to write it down like an XML string:
<DocumentList>
<UnsignedDocument Path="\\some\wehere\file1.txt" Description="Whatever comes here" />
<UnsignedDocument Path="\\some\wehere\file2.txt" Description="Something else" />
...
However, as far as I see in HTML4.01 specification, the PARAM HTML element may not have content and it has no end-tag.
The choices I'm considering, are:
html-encode the xml structure and send it to applet in a single PARAM object
creating a list of PARAM objects and constructing their names like "File1", "Description1", "File2", "Description2", "File3"... then in Java applet create a while loop to read filenames while there is any.
However, none of the solutions seems to be elegant. The question is, what's the best practice in this case?
Pass them comma-separated:
<param id="files" name="files"
value="\\some\where\file.txt,\\some\where\file.txt" />
and then use String.split():
String[] fileNames = param.split(",");
In case of more complex structures you can use JSON to represent them.
You can also provide your applet with a public method:
initFile(String path, String description)
and call this method from javascript code (in which you can loop)
var applet = getElementByTageName(applet);
applet.initFile("yourPath","yourDescription");

Categories