spring mvc form inputs - java

I m having a hard time working with array inputs for example how can I:
Write the following html input using spring form tag:
<input name="phone []"/>
<input name="phone[]"/>
I tried
<form:input path="phone []"/>
but no luck throws error saying that class attribute phone [] does not exists.
2 . After you tell me how to do step 1 how can i repopulate the form with user inputs values using #ModelAttribute In case of validation error. Obviously normal using of <spring:input path="phone"/> won't work cause phone has a list of values.
By the way I use controller with annotations.
Thanks

This depends if you are trying to do dynamic forms or just a static list based forms. For static ones - you need to iterate manually:
<c:forEach items="myModel.phones" varStatus="status">
<form:input path="phones[${status.index}]" />
</c:forEach>
Dynamic forms are a little bit complicated and you should try google as this was asked and answered few times.

Related

JSP send data to Java class

I'm looking to send information after each link click to my java class to help increment a counter and to change an object which is held in my bean (myGame) which holds an array of 'cases'. I understand that I can increment a counter using JS, but i'd like to keep all game logic within the bean if possible.
I have considered using forms and changing the anchor to a submit button, however, I'd like to keep that the player can no longer click the link after it the case is eliminated.
The printCase(int) function works by finding the corresponding case in the array, checking the value of a boolean (is the case is eliminated from the game [deal or no deal]) and then prints the amount of money held in the case if eliminated; the case number if it is not.
I have about a dozen cases.
<jsp:useBean id="myGame" scope="session" class="dealOrNoDeal.GameLogic"/>
<table border="1" id="dndTable">
<form action="/../doStuff.jsp" method="post">
<tr>
<td> <%= myGame.printCase(0) %> </td>
</tr>
</form>
</table>
I've also considered changing my bean to a servlet. Does that work? Can you have a bean in a servlet?
Thanks all.
Update: I've made printCase now print the tag that it's within now, as well. Prints as a submit button if the case is not opened, prints as a <p> if it is (I need it to use the class). Attempted putting the <td>s in there as well, but it somehow messes up the formatting.
I'm now looking to get the name of the one button that was clicked, any ideas?
I'm not sure about understand completely your qestion. But i'll try:
U have 2 ways:
You have to build a jquery where on each button click u call a servlet where u send information to your java class;
Using DWR: http://directwebremoting.org/dwr/index.html

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.

Why should I use the form helper and not write my own html forms

I am learning about play 2.0 and I have got a question on the form helpers.
For me it all comes down to what benefit is it actually giving by using it in the templates? Am I using it correctly?
First: using the form helper:
#form(action = routes.Application.addAccount("blank")) {
#inputText(accountForm("id"))
<input type="submit" name="action" value="submit ID"/><br />
}
Why is that better then just defining
Enter your id "<input type="text" name="id"/>"
I know I can use the form model to help with validation on the server side. - that's where I see the great benefits of form helper. But where does it help to actually include the form in the Scala template? Can i use the form helper to automatically generate useful things in the html like client side validation, etc?
Cheers
it helps you generate a lot more than just tags. From the documentation:
You feed them with a form field, and they display the corresponding HTML form control, with a populated value, constraints and errors
and
A rendered field does not only consist of an tag, but may also need a and a bunch of other tags used by your CSS framework to decorate the field.
(http://www.playframework.com/documentation/2.2.x/JavaFormHelpers)
So instead of
<label for=...>
... error mesages
<input ...
</label>
you have just one readable line
#inputText(accountForm("id"))
EDIT:
It will also read constraints on your java beans, e.g
#Constraints.Required
#Constraints.MinLength(5)
public String firstName;
and use html5 browser validations and display the coinstraints to the user.
(http://www.playframework.com/documentation/2.2.x/JavaForms)

How to use Regex Pattern in entities of Spring Mvc+JPA Application

Can any one suggest me how to use regex patterns in entities, i have done application in springMvc+Jpa ,but my problem here is in UI username and password fields are allowing blank spaces and able to create and update user with blank spaces.i have knowledge in regex patterns ,but i am confusing how to use in spring mvc application ,please help me
Thanks in advance
I suggest reading up some in Springs official documentation. They do have JSR-303 support, where you can annotate certain properties with validation specifications.
http://static.springsource.org/spring/docs/3.1.1.RELEASE/spring-framework-reference/htmlsingle/spring-framework-reference.html#validation
If you only want something simple, you can always post the values to the controller, check your pattern there, and throw an error if it does not pass.
A very simple and crude solution might look like this (i bet someone with more spring experience could improve this a lot). Assume a form bean exists for this page:
jsp:
<form:form *some attributes*>
<ul class="errorMessages">
<c:forEach items="${errorMessages}" var="msg">
<li>${msg}</li>
</c:forEach>
</ul>
<form:input path="myStringNotValidated" id="myStringNotValidated/>
<form:input path="myStringValidated" id="myStringValidated/>
<input type="submit" value="submit"/>
</form:form>
controller:
List<String> errorMessages = new ArrayList<String>();
#RequestMapping(value="someURLYouPostedTo", method = RequestMethod.POST)
public ModelAndView save(#ModelAttribute myFormBean){
if(!*check your regex here on myFormBean.getMyStringValidated()){
errorMessages.add("Bad value on x");
// Make sure error messages is available in the model map
// Return to the same page
}
}

How do I bind HTML table data to a java object in a spring controller?

I have a spring MVC application using JSP as my view technologies with Jquery for AJAX. I have a table such as the following:
<table>
<tr>
<td>name1</td>
<td>value1</td>
<td>setting1</td>
</tr>
<tr>
<td>name2</td>
<td>value2</td>
<td>setting2</td>
</tr>
</table>
I need to serialize this table so that it can later be bound to an object in my controller. However the jquery serialize() method only works on form fields. What would be the best approach to get the table data into the HTTP request so that I can later bind it to a java object?
EDIT:
I have a java object that has a collection so
class MyOject {
private List<AnotherObject> items = new ArrayList<AnotherObject>();
// standard getters and setters
}
class AnotherObject {
private name;
private value;
private setting;
// getters and setters
}
In the screen the user is creating new items on the fly. When the user is done, they submit the form and then I need to process all the items in the list and instantiate a new collection with those items.
For display purposes I am creating a new table row when an item is created.
The <Form> tag is how you tell the browser "Put this stuff in the web request." That's how you get object binding in Spring. What is your reason for not using a Form? You don't necessarily have to put it in a form in the page, you could give your table elements IDs and fetch their contents in the javascript if you really needed to.
Edit: I think maybe it's hard to answer because it's not clear why you want the browser to give you back things that you gave it in the first place. Maybe what you really need is the #SessionAttributes() annotation on your controller so that you can preserve State of the original page shown to the user?
More Edit:
kk, see now. If what you want is Spring web data binding then create a form in parallel as you add more table rows. e.g.,
<form id="myObject" action="whateverYouNeedHere.htm" method="post">
<input type="hidden" id="items[0].name" name="items[0].name" value="foo"/>
<input type="hidden" id="items[0].value" name="items[0].value" value="bar"/>
<input type="hidden" id="items[0].setting" name="items[0].setting" value="buzz"/>
<input type="hidden" id="items[1].name" name="items[1].name" value="foo"/>
<input type="hidden" id="items[1].value" name="items[1].value" value="bar"/>
....
Then just submit that and it will bind right on for you. If you did mean to handle the content yourself, then you probably could use XHR as someone else mentioned.
Use the Spring Data Binding and Validation API to bind it into a Java object of your own design. That documentation is web-agnostic; check out the later chapter to see how the web tier leverages it.
You'll want an abstraction beyond a table, I presume.
in order to stick your table information into a java object, you will first need to send it to the server.. for that you will need to either send it via XHR or in a form.
in order to serialize the object you will need to write some javascript/jquery.
i could write it for you, but your requirements are somewhat vague when it comes to how your table will look, nor do i want to guess about what the java object you want to add your data to looks like.

Categories