In the input field of the jsp page we are giving some value with special characters Arągł but those are not coming as given in the form. The value of the String query should be same as given but it's some thing like ArÄgÅ
Server side code:
public void sendForm(PortletRequest request, Model model) throws UnsupportedEncodingException {
Map<String, String[]> renderParams = request.getParameterMap();
for (Entry<String, String[]> entry : renderParams.entrySet()) {
final String key = entry.getKey();
final String[] c = entry.getValue();
if (c != null && c.length > 0) {
if(key.contains("query")) {
String query = c[0];
}
}
}
}
JSP code:
<%# taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<portlet:defineObjects />
<portlet:actionURL var="formUrl" name="sendForm"/>
<form name="editForm"
method="POST"
action="${formUrl}"
>
<input type="text" name="query" id="query"/>
<input value="Submit" type="submit" />
</form>
In the input field of the jsp page we are giving some value with special characters Arągł but those are not coming as given in the form. The value of the String query should be same as given but it's some thing like ArÄgÅ
Related
I´m trying to create a form and validate its data via #Valid on the command object.
The validation performs well, but an error is ocurring going back to web.
This is what I have:
HTML
<div id="content" layout:fragment="contenido">
<div sec:authorize="isAnonymous()">
<form class="form-horizontal" action="#" th:action="#{register}" th:object="${userForm}" method="post">
<input type="hidden" name="_csrf" th:value="${_csrf.token}"/>
<fieldset>
<label for="alias" th:text="#{form.register.alias}">Alias</label>
<input id="alias" type="text" th:field="*{alias}" placeholder="Su alias" required="required" autofocus="autofocus"/>
<label for="pass" th:text="#{form.register.password}">Contraseña</label>
<input id="pass" type="password" th:field="*{password}" pattern="[\w\d-_]{5,15}" required="required" th:title="#{form.error.password}"/>
<p th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Error en el dato ingresado</p>
<button type="submit" name="save" class="btn btn-primary" th:text="#{control.register}">Registrarme</button>
</fieldset>
</form>
</div>
</div>
Controller
#RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST)
public String register (final ModelMap model, #Valid final UsuarioForm userForm, final BindingResult result) {
if (result.hasErrors()) {
return "register";
} else {
return "redirect:/" + HomeController.PAGE_NAME;
}
}
When Clicking on "submit" the "register" method is called, result.hasErrors() is true so the same page should be displayed, but this error occurs.
Stack
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:396)
org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:323)
org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:289)
org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98)
org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017)
org.thymeleaf.dom.Node.processNode(Node.java:972)
If I add "userForm" to the model in the Controller this way:
Controller Modified
#RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST)
public String register (final ModelMap model, #Valid final UsuarioForm userForm, final BindingResult result) {
if (result.hasErrors()) {
model.addAttribute("userForm", userForm); //THIS LINE IS ADDED
return "register";
} else {
return "redirect:/" + HomeController.PAGE_NAME;
}
}
The error disappears, BUT... the expression in the HTML ${#fields.hasErrors('password')} results false, so I cant show the error messages to the user.
Any idea of why this behaviour is happening?
Thanks in advance!
PS: I am using Spring MVC 4.1.2 with Thymeleaf 2.1.4
This
public String register(final ModelMap model,
#Valid final UsuarioForm userForm,
final BindingResult result)
should be:
public String register(final ModelMap model,
#ModelAttribute("userForm") #Valid final UsuarioForm userForm,
final BindingResult result)
Notice the #ModelAttribute annotation.
I have a couple of Extension classes, borrowed from various places, and they both work - individually. When I try to use both on the same page it appears one does not work. Here is the setup:
MVC 2 (no path for upgrading it to MVC 3 or 4)
HtmlPrefixScopeExtensions - http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
FileBoxHtmlHelperExtension -
http://forums.asp.net/p/1566760/4033836.aspx
The .ascx page code looks like:
<%# Control Language="C#" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewUserControl<PB.WMATA.ApplicationServices.ViewModels.Files.CIPDocumentAndFile>" %>
<%# Import Namespace="Company.Web.Extensions"%>
<div class="editorRow">
<% using(Html.BeginCollectionItem("docs")) { %>
<%= Html.Hidden("CIPDocument.Id", (Model != null) ? Model.Id : 0) %>
<label for="CIPNumber">Document Name:</label>
<%= Html.TextBox("CIPNumber", (Model != null) ? Model.CIPNumber : "", new { #size = "50", #maxlength = "255" })%>
<%= Html.ValidationMessage("CIPNumber")%>
<% if (Model != null && Model.FileName != null && Model.FileName.Length > 0) { %>
<label>Current File:</label>
<%= Model.FileName %>
<% } else { %>
<label>
File Upload:
<%= Html.FileBoxFor(m => m.HttpPostedFileBase) %>
</label>
<% } %>
delete
<% } %>
</div>
The output for this looks like:
<div class="editorRow">
<input name="docs.index" autocomplete="off" value="1809201d-2143-4da3-ba34-e443a332c516" type="hidden">
<input id="docs_1809201d-2143-4da3-ba34-e443a332c516__CIPDocument_Id" name="docs[1809201d-2143-4da3-ba34-e443a332c516].CIPDocument.Id" value="0" type="hidden">
<label for="CIPNumber">
Document Name:
</label>
<input id="docs_1809201d-2143-4da3-ba34-e443a332c516__CIPNumber" maxlength="255" name="docs[1809201d-2143-4da3-ba34-e443a332c516].CIPNumber" size="50" value="" type="text">
<label>
File Upload:
<input id="HttpPostedFileBase" name="HttpPostedFileBase" type="file">
</label>
<a href="#" class="deleteRow">
delete
</a>
</div>
Notice the FileUpload control did not get the HtmlPrefixScope. I expected it to be:
<input id="docs_1809201d-2143-4da3-ba34-e443a332c516__HttpPostedFileBase" name="docs[1809201d-2143-4da3-ba34-e443a332c516].HttpPostedFileBase" type="file">
I am not quite savvy enough with extensions to see what may be going on. I suspect that the collection extension is being handled before it tries to handle the filebox extension. Any ideas?
After digging in it turns out that I needed to get at the TemplateInfo.HtmlFieldPrefix value as the Html.BeginCollectionItem("docs") call had altered it. Was really simple once I understood the lifecycle of the TemplateInfo object. Here is the altered code for the FileBox & FileBoxFor code pieces:
public static MvcHtmlString FileBox(this HtmlHelper htmlHelper, string name, IDictionary<String, Object> htmlAttributes)
{
// If the HtmlFieldPrefix has been altered (see HtmlPrefixScopeExtensions class!!) then this will work with it...
var htmlFieldPrefix = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix;
name = (!string.IsNullOrEmpty(htmlFieldPrefix) ? string.Format("{0}.", htmlFieldPrefix) : "") + name;
var tagBuilder = new TagBuilder("input");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttribute("type", "file", true);
tagBuilder.MergeAttribute("name", name, true);
tagBuilder.GenerateId(name);
ModelState modelState;
if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState))
{
if (modelState.Errors.Count > 0)
{
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
}
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
}
I have problem with uploading my file. I want to upload it from my edit view:
<%
using (Html.BeginForm("edit","profile",FormMethod.Post, new { enctype="multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true) %>
<%: ViewData["ErrorMessage"] %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
<input type="file" id="Image" name="Image" />
<label id="LabelErrorImage" class="errorMessage" />
</div>
<p>
<input type="submit" value="Save" onclick="return Validate(); return false;"/>
</p>
</fieldset>
<% } %>
I want to use HttpPostedFileBase class. My edit action:
[Authorize]
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(string id, HttpPostedFileBase file, FormCollection formValues)
{
if (ModelState.IsValid)
{
if (file != null && file.ContentLength > 0)
{
CustomHelpers.createFolder();
var tmpPath = MyConfig.UPLOAD_FILE_PATH + "/" + Membership.GetUser().ProviderUserKey.ToString();
var path = Path.Combine(Server.MapPath(MyConfig.UPLOAD_FILE_PATH), "Avatar");
var fileExtension = Path.GetExtension(file.FileName);
file.SaveAs(path);
user.Image = "Avatar";
}
adventureDB.SaveChanges();
return RedirectToAction("Index");
}
}
But I always have empty the file object, why????? Do you have any ideas, suggestions why it can work like that? Maybe there is problem how I pass on the file value to my Edit action?
EDIT:
IT IS REALLY STRANGE AS EVEN WHEN I REMOVE
using (Html.BeginForm("Index","Profile",FormMethod.Get, new { enctype="multipart/form-data" }))
The page source still has:
<body>
<form method="post" action="6111e591-b92d-4bcb-b214-ab8f664b35f9" id="form1">
I mean I can not change the tag but have no idea why :/
Try changing:-
public ActionResult Edit(string id, HttpPostedFileBase file,
FormCollection formValues)
to:-
public ActionResult Edit(string id, HttpPostedFileBase image,
FormCollection formValues)
as the name of your input is image
<input type="file" id="Image" name="Image" />
edit
To be honest something else is stopping the binding of image. Is this the whole form that you have posted?
A few things to test
You have HTTPOST decorating your method twice, although I don't believe this should make a difference.
View the source and make sure there is nothing else named name=image in the source.
Make sure you empty your cache and make sure source is correct before testing again
Try using <form action="/profile/index" method="post" enctype="multipart/form-data">
Judging by your last edit you have a problem with master pages/layout? Is this a mvc/webforms hybrid?
The solution of this problem when:
We use Master.Site,
We want to upload file in a view,
We are sure that it should work but we all the time has null,
Then:
Guys were right - I had wrong name in my view - check it!
Check source code of your view and if you have 2 < form > tags you should remove the < form > tag from Master site as then the second one is ignored!
Now it should work.
Well, in your view you named the file input 'image' but your action method accepts a parameter called 'file'. Rename one of those and it should work.
I cannot make sense of this...
The simplified code below OUGHT to insert a new form field of the current time's second value.
Instead, even though the list manipulation happens correctly (as verifiable within the controller and in the debug output), the View does render an additional element, but seems to be just a copy of the final field's value (prior to submit). This can be verified by changing a field prior to submit - the values stick and the new element is a duplicate of the submitted final value.
The part that really cooks my noodle is that if I trivially change the operation from an Insert() to an Add(), the Add() works as expected!!
Using this example Model:
public class MyViewData
{
List<string> stringData = new List<string>();
public List<string> StringData { get { return stringData; } }
}
And this Controller:
public class TestController : Controller
{
public ActionResult Index()
{
return View(new MyViewData());
}
[HttpPost]
public ActionResult Index(MyViewData data)
{
data.StringData.Insert(0, DateTime.Now.Second.ToString());
return View(data);
}
}
And this View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Forms.Controllers.MyViewData>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Test
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%
System.Diagnostics.Debug.WriteLine("---");
for (var i = 0; i < Model.StringData.Count; i++)
System.Diagnostics.Debug.WriteLine("{0}", Model.StringData[i]);
%>
<% using (Html.BeginForm()) { %>
<% for (var i = 0; i < Model.StringData.Count; i++) { %>
<%: Html.TextBoxFor(model => model.StringData[i])%></br>
<% } %>
<div><input type="submit" value="Do it" /></div>
<% } %>
</asp:Content>
This is the normal behavior of standard HTML helpers and is by design. When rendering the input field they will first look at the request POSTed values and only after in the ViewData and view model. This basically means that you cannot change POSted values in your controller action.
So if you have a form with an input field:
<%= Html.TextBoxFox(x => x.Id) %>
which is posted to the following action
[HttpPost]
public ActionResult Index(MyModel model)
{
model.Id = "some new value";
return View(model);
}
when rendering the view back the html helper will use the posted value. You could either write a custom html helper that does the job for you or handle it manually (absolutely not recommended but for the record):
<input type="text" name="StringData[<%= i %>]" value="<%= Model.StringData[i] %>" />
I am passing the edited value and retrieving it back but I dont get back the id value.. how do I get back the value for Id...The id value will actually remain the same..all I need to do is pass it back again...
public ActionResult EditValue(int id)
{
ViewData["id"]=id;
ViewData["Value"]=GetOriginalValue();
return View();
}
[HttpPost]
public ActionResult EditValue(string Value)
{
//How do I get the id value back..it will always be the same
UpdateValue(id,Value);
}
//---VIEW--
<% Html.BeginForm(); %>
<div class="editor-label">
<%= Html.Label("Value")%>
</div>
<div class="editor-field">
<%= Html.TextBox("Value",ViewData["Value"])%>
<%= Html.ValidationMessage("Value")%>
</div>
<p>
<input type="submit" value="Update" />
</p>
<% Html.Hidden("id", ViewData["id"]);%>
<% Html.EndForm(); %>
if you put int id into your parameters for the post, it should pick up the hidden form element... Or you can use the FormCollection class and access the individual form elements.
So your method signature would look like:
public ActionResult EditValue(int id, string Value)
or
public ActionResult EditValue(FormCollection form)