Spring3 ModelAttribute from jsp request params - spring

Is it possible to grab various form components and turn them into their corresponding DAO objects? I'm trying to avoid creating a custom Bean (which encapsulates all fields) for each Form.
Ex:
<form action="/add">
<input name="foo" id="foo" value="29"/>
<input name="bar" id="bar" value="63"/>
</form>
public void add(#ModelAttribute("foo") Foo myFoo, #ModelAttribute("bar") Bar myBar)
Currently, I'd have to use the HttpRequest, extract the values (29,63), and use them as primary keys to look up the correct database objects. I didn't know if there was a quick way to auto-wire this using ModelAttribute.
Thanks!

You should use #RequestParam
public void add(#RequestParam("foo") Foo myFoo, #RequestParam("bar") Bar myBar) {
}

Related

Spring MVC <form:xxx> tags

I have a doubt that in our web applications generally we are using spring MVC and for view purpose Spring MVC tags, So while writing Spring MVC tags we are using <form:xxx> tags like <form:input> <form:option> <form:select> also <form:label> like this. My confusion is that generally in HTML we are using all these above tags without including <form:xxx> so what is the exact difference in between both the tags and why <form:xxx> tag is needed when it comes to Spring MVC. Is there any object associates to it or what?
If you want to bind the user values collected using form to Object, Then you will need to use Spring form tag. For example..
<form:form action="actionUrl" method="post" modelAttribute="loginForm">
<form:input path="empId" placeholder="Enter Employee Id"/>
<form:errors path="empId" cssClass="error"/>
<form:password path="password" placeholder="********"/>
<form:errors path="password" cssClass="error"/>
<input type="submit" value="Login"/>
</form:form>
Here modelAttribute="loginForm" , loginForm is a Object of Java Class with two fields empId and password.
public class LoginForm{
private Integer empId;
private String password;
//getters and setters
}
So, Now when you submit the Login form, the values are automatically bound to java object which you can access in your controller method. Like
#RequestMapping(value = {"/login"},method = RequestMethod.POST)
public String authLogin(#Valid LoginForm loginForm,BindingResult result,ModelMap model){
//Your code logic
}
So, spring form binds form values automatically to java object and saves from accessing each request parameter manually. These values can also be validated using validation framework.

Desing of form in spring

I am using spring framework. This question mainly concerns about design and implementation.
In my project, I have to use many forms and most of them are different. What is the recommended way of implementing forms in spring. Using Model?
I want to use ajax for forms submissions. The forms in the project are really huge having 8-15 fields. Is it a good way to use ajax for such huge forms? If yes, how can I do it? Can I use model attribute?
A typical way to do this is to use form backing objects. For example, with a form like
<form>
<input type="text" name="username">
<input type="password" name="password">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
You would create a DTO class
public class UserForm {
private String username;
private String password;
private String email;
public UserForm() {}
// getters and setters
}
Then your #Controller handler method can map as
#RequestMapping(method = RequestMethod.POST)
public String handleFormSubmit(#ModelAttribute UserForm userForm /*, more */) {
/* logic and return */
}
And Spring will be able to bind the value from the name attribute of input elements to the fields of the class.
AJAX doesn't care about the size of your forms.

Use Spring Message Tag in Attributes - The best way?

It's not a possible to use a tag in an attribute:
<input type="submit" value="<sp:message code="send" />" />
There's no way to use this message tag as a function:
<input type="submit" value="${sp:message('send')}" />
The only way is to save the message in a var (as mentioned here), but I think using a function would be much better than this:
<sp:message code="send" var="tmp_send" />
<input type="submit" value="${tmp_send}" />
So I would like to write my own function:
<taglib xmlns...>
<function>
<description>Be able to use Spring messages in attribute values</description>
<name>message</name>
<function-class>com.example.MyTagLib</function-class>
<function-signature>String message(java.lang.String)</function-signature>
</function>
</taglib>
But there's a design problem accessing MessageSource here (as mentioned here).
public class MyTagLib {
public static String message(String code) {
// TODO: use messageSource to resolve message
return code;
}
}
Isn't there a better way to acess spring messages in attributes?

Backing Object not removed after Form submission in Spring Mvc

In Spring MVC-3 , when we do a form submit, the form backing object does not get removed.
I thought in spring mvc, it removes the baking object after a form submit.
Am I correct or Can anyone explain what might have happened here ?
PS
<form:form id="id1" commandName="command1" modelAttribute="command1" method="post">
When we do a submit, that model attribute binned to the form get removed at some point from the session doesn't it?.
What I want to know is that point at where the command object get removed.
It would be helpful if you could provide some code. Might be a bug there.. MVC actually does not have a backing object. All you can do is put objects into the model map and they get deleted after each request. When you do a form post, all Spring MVC does is to map your form inputs to the object you have in your controller:
form:
<form action="/some-path" method="post">
<input type="text" name="some_property" />
<input type="submit" value="Submit" />
</form>
controller:
#RequestMapping(value="/some-path" method = RequestMethod.POST)
public ModelAndView createItem(SomeObject someObject, BindingResult result) {
// ...
}
mapping object:
public class SomeObject {
private String some_property;
// getter, setter
}
If you didn't declare your object as a Bean (#Named, #Component or something) or added it to the model map:
ModelAndView mav = new ModelAndView();
mav.addObject("someObject,someObject);
then it'll be gone when the controller is finished.
edit
Didn't notice the jsp tag.. Don't think it changes much.. Still, without any code it's hard to say anything for certain.

when using spring autowiring then struts tag does not map with action class

when i am putting autowiring interceptor(interceptor-ref name="autowiring") in the action tag in spring.xml then struts tag in index.jsp does not able to map with the setter of that action class. and when i am removing that autowiring tag from the action tag then in that case struts tag of jsp does able to map with the setter of the action class.
Any help from your side will be more than welcome. if you need any sort of example then let me know.
<s:textfield name="name" label="Name" />
<s:textfield name="salary" label="Salary" />
<s:submit value="Add Record" />
then this struts tag with name 'name' and 'salary' does not set the value in action class represented with the same name of setter/getter.
public void setName(String name) {
this.name = name;
}
public void setSalary(String salary) {
this.salary = salary;
}
If you define an interceptor On an action you must define all interceptors on an action. If your parameters aren't being set then whatever the "autowiring" interceptor reference doesn't include the "parameters" stack, the interceptor responsible for transferring form properties to actions.
And Umesh is correct, if you're using Spring, the plugin handles injection for you, and you do not need to manually define the "autowiring" plugin on your action. If you just remove that interceptor definition, your parameters should be set as normal, and the action should still be wired up.
That said–using a session factory manually inside an action would not be considered a best practice. Any session factory logic should be wrapped up inside your DAOs/services/etc. Actions should rarely (read: never) be aware of the persistence layer.

Resources