This may be a very basic question but I am confused. I have couple of doubts:
In spring form <form:form if method is not specified then is it GET or POST?
If a spring form has <form:form with commandName then is that GET or POST?
The second question is because I see a "form:form commandName=xyz action=abc" in the code
When I check the HTML code (view source) it translates to
"form action=abc method=POST"
Please help me with this.
HTML form without specified action is always GET. It's HTML standard.
http://www.w3.org/TR/html401/interact/forms.html#h-17.3
But when you look inside FormTag in Spring source you'll notice this code:
public class FormTag extends AbstractHtmlElementTag {
/** The default HTTP method using which form values are sent to the server: "post" */
private static final String DEFAULT_METHOD = "post";
So for spring tag <form:form action is post by default.
commandName is just name for model attribute binded with your form. It has nothing to method type. Moreover, it's equivalent to modelAttribute so you can use either.
Spring form has default method as POST. If you want to do the get, you have to write, method="get" in form:form tag.
Related
I have two portlets :
1. Blog Portlet.
2. Author Portlet.
I used the concept of Public render parameter to send data (say key "urlTitle") from Blog portlet to Author portlet
But after sending "urlTitle" from Blog portlet how can I remove the data from Public render parameter
In Blog Portlet
EX code: view.jsp
<portlet:renderURL var="viewEntryURL">
<portlet:param name="struts_action" value="/blogs/view_entry" />
<portlet:param name="redirect" value="<%= currentURL %>" />
<portlet:param name="urlTitle" value="<%= entry.getUrlTitle() %>" />
</portlet:renderURL>
Send Data
Now how I can remove "urlTitle" form the public render parameter after data is sent.
Please give feedback.
-Thanks in advance
You could think about the following:
The LiferayPortletURL (the class that models portlet render, action and resource URLs tags) offers a method called setCopyCurrentRenderParameters
https://docs.liferay.com/portal/6.2/javadocs/com/liferay/portal/kernel/portlet/LiferayPortletURL.html#setCopyCurrentRenderParameters(boolean)
which when set to false, avoids copying render parameters, and the URLs are "cleaned" from those.
The caveat with this is that you would need to create a LiferayPortletURL in the back end doing the following:
LiferayPortletURL renderUrl = PortletURLFactoryUtil.create(
httpServletRequest,
themeDisplay.getPortletDisplay().getId(),
themeDisplay.getPlid(),
PortletRequest.RENDER_PHASE);
renderUrl.setCopyCurrentRenderParameters(false);
and after that pass it to your JSP set as an attribute (maybe renderRequest.setAttribute("renderUrl",renderUrl)?). I haven't done this for render URLs, but for resource URLs and it works!
You need to set
javax.portlet.init-param.copy-request-parameters=false
in your portlet class.
I have a form and post data into controller via Thymeleaf:
<form action="lia.html" th:action="#{/lia}" th:object="${myRequest}" method="post">
At another place of my html page, if a user click a particular button, I want to modify that object and send it to same controller.
I have already that object which has been initialised. Button is not a part of any form. How can I send that object into a controller with Thymeleaf.
PS: I know that I can send it via Javascript or put such buttons into a form but I want to learn the Thymeleaf way.
I think the only similar approach to what you're looking for is using bind with Spring EL expressions.
Thanks to the advanced form-field binding capabilities in Spring MVC,
we can use complex Spring EL expressions to bind dynamic form fields
to our form-backing bean. This will allow us to create new Row objects
in our SeedStarter bean, and to add those rows’ fields to our form at
user request.
Take a look the next link, where is good example:
http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields
The button
<button type="submit" name="removeRow"
th:value="${rowStat.index}" th:text="#{seedstarter.row.remove}">Remove row</button>
Request Mapping
#RequestMapping(value="/seedstartermng", params={"removeRow"})
public String removeRow(
final SeedStarter seedStarter, final BindingResult bindingResult,
final HttpServletRequest req) {
final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
seedStarter.getRows().remove(rowId.intValue());
return "seedstartermng";
}
I am trying to modify and send a list of objects using spring MVC.
I want , when I will click in the button that triggers the form submit, in the controller, capture all the list with the objects.
Unfortunately.. I have no idea how to do this.
So far I got this:
<#form:form modelAttribute="MyObject" method="post" action"somewhere">
<#display:table list=myList id="myObject">
<#display.column title="Name">
${myObject.name}
</#display>
<#display.column title="value">
HERE I SHOULD MODIFY EVERY OBJECT WITH AN INPUT, NOT SURE IF THIS WOULD WORK
<form:input path=${myObject.value} id="value" />
</#display>
</#display>
</#form:form>
And at my controller so far I have this:
#RequestMapping (value="myurl" method = Request.POST)
public void myMethod(#ModelAttribute Object myObjects){
So here Id like to get the entire list of objects, but myObjects is null.
}
Any idea how to do this?
Thanks
I am new to Spring MVC (and front end for that matter). I have a jsp with a form on. In my controller's GET method I add the command to the ModelMap. The page does some validation (greys out things when checkboxes clicked, etc). Then I go to the next page. The user is suppose to be able to click the back button (which is wired up in an tag - for graphics reasons apparently) and then make changes to their form. Except...the form is empty.
So my main question - what is the best way to go back (to my .do) and retain all the values in the form? There are some things that runs on my GET method...so this still needs to happen.
What I tried: I read somewhere that the command is suppose to pre-populate the form? So I allready have a command which I use to get the info....this is what I did (but it doesn't work). (I debugged and the command is populated with the values)
<form class="form-horizontal" commandName="myCommand" name="formdetail" id="formdetail" method="post">
Controller
#RequestMapping(method = RequestMethod.POST)
public View handleSubmit(#ModelAttribute MyCommand myCommand, BindingResult result, HttpServletRequest aHttpServletRequest){
WebUtils.setSessionAttribute(aHttpServletRequest, "goalDetailCommand", goalDetailCommand);
//Then do some redirecting
#RequestMapping(method = RequestMethod.GET)
public String show(ModelMap model, HttpServletRequest aHttpServletRequest, #ModelAttribute MyCommand myCommand) throws Exception {
myCommand = (MyCommand)WebUtils.getSessionAttribute(aHttpServletRequest, "myCommand");
model.addAttribute("myCommand", myCommand);
Thanks
EDIT:
I didn't have the path part in. Added it but still no luck. Is something else wrong?
<input type="text" class="amount input-medium" path= "amountToSave" id="amountToSave" name="amountToSave" placeholder="0000.00">
I debugged and the command is populated with the values
its working as expected, just reference the command object's values/fields in the form, if the command object has a getTitle for example do this :
<form:input path="title" maxlength="90" id="title"/>
What is the defined behavior for form binding in ASP.NET/MVC if you POST a form and its action has query parameters and you have form data?
For example:
<form action="my/action?foo=1" method="post">
<input type="hidden" name="bar" value="2">
</form>
If such a form is submitted should the controller get both foo and bar or only one of them?
The controller will get both values. The default model binder will try to find matches for the parameters from both the URI (either query string or route parameters) or the body (and forms data is supported out-of-the-box).
Note, you can see this is supported by Html.BeginForm helper, you do so through routeValues:
#Html.BeginForm("ActionName", "ControllerName", new { foo = "1" })
It essentially generates the same html as your form tag, but wanted to post for those who find this question and want to know how to pass additional values that are not part of the form using the BeginForm helper.
I think it should be able to get both. In this case, I would create a ViewModel that contains two string or int properties, one named 'foo' and the other named' bar' and have your ActionResult accept the ViewModel. You should see both values come in.