I have a form which takes client's user name and machine name. I want to display machine's username and hostname in a textfield as a default value. How can I set string values to default_username and default_hostname so that <s:textfield value="default_username">
and <s:textfield value="default_hostname"> will display defaults?
Well if you are browsing them they must be at value stack when your page got displayed all you need to refer them inside your text-field using OGNL something like
<s:textfield value="%{default_username}" name=username>
make sure that the default_username should be in value-stack which means your action should have public getter and setter for default_username.
Additionally your question is not clear enough so a more detail about use-case will help to get better inputs.
Related
I'm a just starting to learn Play, so this is a basic question, but I've searched every term I can think of and can't find the answer.
All I want to do is have a page that on submit takes an ID from a text field and puts it in the URL directly (e.g. /myservice/person/123). Instead, the URL being generated contains the ID as a parameter (e.g. /myservice/person?id=123).
I know my controller is being invoked correctly if I type the URL in by hand, so I'm inclined to think my routes file is correct. This is what my entry looks like:
GET /person/:id controllers.PersonActions.getPerson(id: String)
So I'm assuming something is going wrong in my template, which looks like this:
#form(routes.PersonActions.getPerson(personID)) {
#* #inputText(personForm("id")) *#
<input type="text" name="id" value="#personID">
<input type="submit" value="Get">
}
You can see that I've commented out another way using #inputText also, but it behaves identically for me. I'm not tied to either method.
I've also tried this using POST, which removes the ID from the URL entirely, which I don't understand either, but since I'm just doing a simple query, I'd rather use GET.
Can anyone help me understand what's going on here? I feel like there's something fundamental about how routing/URLgeneration works that I'm not understanding, but I've gone through the tutorial and the docs so many times today I'm at a loss.
Thanks in advance.
Oh, and I'm using Java 7, Play 2.1, and Eclipse Kepler
I think you are trying to skip a step and map form request data from a view directly to a controller method. You will need to submit the form, have a controller parse the data and then render the next appropriate view based on the data you parsed from the form.
Remember that the personId in your view is a parameter that is bound server-side when the view is rendered. In this case, the submit url of the form is hard coded to whatever personId is passed in to the view at render time -- it doesn't change dynamically when the input box changes.
To fix, add a controller method to accept requests from /person (I'm guessing this based on the part in your question that says the form is being submitted to /person?id=123, in any case it should be the URL of the form you've shown above)
e.g. if you want to use a GET method for the form add:
GET /person controllers.PersonActions.findPerson(id: String)
and in your PersonActions controller (I'm assuming you're using Java, if scala I'm sure you can adapt it from the docs)
public static Result findPerson(String id){
/*
* I'm assuming this method exists and works because you say
* when you type in /person/123 manually the routing works
* and your router says getPerson is the method name for that URL.
*/
return getPerson(id);
}
Can I preserve object across actions in Struts2 without using session?
I'm working on a Struts2 project and curious if there are some ways to preserving object
value once an action is ended.
What I'm trying to do is:
Calling an action to read from an uploaded file and prepare its contents as a list of objects.
Then I display the list as an example for the user.
Then the user can click on a submit button to call an action to process the list which is created from the first action.
Usually the list would be lost once the action is end. So, I have to store the list as a session, but I think it should have some better methods to achieve the I'm working on.
If you want to preserve data across requests the session is the normal mechanism for doing so.
If you don't use the session you'd need to essentially duplicate its functionality to maintain the data associated with the current user/conversation.
If you need to preserve the List, then you have to use the Session.
But if you (if I've understood your problem) just need to handle the List through
ActionOne (that creates the List) ->
JSPOne (thast shows the List to the user) ->
ACtionTwo (that receives the List from JSPOne and does some business with it)
, without having to worry about the fact that the user can change the List client-side (for example manipulating the web page with FireBug), then you don't need the session.
You just need the List object to be declared on ActionOne and ActionTwo (with getters and setters, at least the getter on ActionOne and at least the setter on ActionTwo), and to include List name and index on name attribute of JSP tags.
If you just draw it (like with <s:property/> tag), instad of using some tag that will post the value, like <s:textfield />, then just use an <s:hidden /> to post te value.
For example, if you have:
List<MyObject> myList //with getters and setters
and assuming that MyObject has id and value fields (with getters and setters),
in JSP instead of
<s:iterator value="myList" >
<s:property value="id"/>
<s:textfield name="value"/>
</s:iterator>
use
<s:iterator value="myList" status="ctr" >
<s:hidden name="myList[#ctr.index].id" value="id"/>
<s:property value="id"/> <!-- no need here, you won't post this -->
<s:textfield name="myList[#ctr.index].value" value="value" />
</s:iterator>
You will eventually validate a List like this throught the Visitor Validator (somewhewre in the future after all the rest is done :)
Enjoy
I'm using MVC for my data entry form and I have the following div:
<div>
<label>Bar Code:</label>
#if (Model.GiftCardId == default(int))
{
#Html.TextBoxFor(model => model.BarCode)
}
else
{
#Html.TextBoxFor(model => model.BarCode, new { #readonly="readonly"})
}
</div>
Here, I'm making sure that if the user is entering a new gift card, an editable input is displayed to allow the user to enter a new bar code. But if the user is editing an existing gift card, the input must display as a readonly input. My question is: can the user alter the readonly attribute of the barCode input and allow himself to enter a different one? The BarCode field is not the primary key in the table but it must be unique. I use the GiftCardId field to identify the record. But then, what's to stop the user from changing the GiftCardId as well when submitting the form? How can this be controlled?
I understand this to be a security-related question: ie. can the user hack the form to do something with it that you didn't intend.
The answer is yes, a user can use tools like Firebug to interfere with the markup, thereby changing the readonly attribute.
You don't show how the GiftCardId is collected from the user. Assuming it is collected and validatated in a previous view / action method, a more secure approach would be to redirect to a different view depending on whether the GiftCardId is valid / new or not.
Edit after comments
A couple of suggestions.
Store the GiftCardId in session state rather than send it to the browser.
Use a one-way hashing function to generate a token from the GiftCardId and send it to the browser in a hidden field. Rehash the GiftCardId that is posted back and check that it matches the original hash. See this short article on creating an MD5 hash.
simple answer is "Yes", all request can be forged, that's why you should never trust user inputs and validate the user inputs on the server side.
What you can do really depends on what you needs and the implication of the GiftCardId been modified. Things you could do in addition to the server side validation,
1. hide the field instead of making it visible
2. encrypt the GiftCardId
In a work around I've used in the past, I've used the id field to pass an additional parameter that I needed. But I need to pass three parameters through a remoteField and now am presented with the fact I need to find a way to pass these parameters:
<g:remoteField action="updateFields" update="theDiv" id-"${personInstance.id}" paramName="search" name="updateFields" value="" />
Need: The search field (search), the person id (id), and now I need the company the person works for (c_id).
I can do something like this:
<g:remoteField action="updateFields" update="theDiv" id-"${personInstance.id}" paramName="search" name="updateFields" value="" params="${[c_id:c_id, search:/'+this.value+/']}"/>
If I try to obtain the search value with the params, the search field is now '+this.value+'. Can I just pass the object search field as an addition param in the map (like above) by referencing this.value? If so, what am I doing wrong, since my gsp doesn't load.
Edit
My current work around is to tie both IDs in a ID field, split by a delimiter and then broken into an array once it reaches the controller (obviously not ideal!)
Although I don't use remoteField, I do use remoteFunction frequently and have found I can use multiple javascript based variables directly with the 'params' parameter. E.g.
<script>
function someJSFunction(id1,id2,id3) {
<g:remoteFunction action="ajax_function" params="{id1:id1,id2:id2,id3:id3}" update="someDiv"/>
}
</script>
Hope that helps.
Lets say I have an Issues domain class and it has as a field assignedTo:
String title
String priority
User assignedTo
...
I need to be able to sort on assignedTo. Neither the list.gsp default scaffolding nor the tag it uses, g:sortableColumn, support this. It seems like the g:sortableColumn needs to have both a property field, and a propertyOfProperty field.
Do you know a good way to solve this?
Ok, so this appears possible, just missing clarity in the documentation, and searching the web didn't help.
So, one can do property="assignedTo.lastName", i.e.
<g:sortableColumn property="assignedTo.lastName" title="${message(code: 'issue.assignedTo.label', default: 'Assigned To')}" />
Ray's solution will work but if assignedTo is a nullable field, any results with assignedTo set as null won't show up in your result list
This grail's solution is a workaround:
http://www.grails.org/version/GSP+Tag+-+sortableColumn/2
Of course if it is a required field, or you don't care about not showing results without the assignedTo variable then use property="assignedTo.lastName"