add attributes in HTML from property file in struts2 - struts2

My action class is extending ActionSupport class from "com.opensymphony.xwork2.ActionSupport". I want to add an attribute of html tag inside JSP using properties file. below is an example.
<label>
<s:text name="getText('abc.def.userID')" />
</label>
<input name="useralias" id="useralias" autocomplete="off" type="text" class="textfield" maxlength="Value From PropertyFile"/>
I want to set maxLength property using property file. Above, getText('abc.def.userID') is being translated successfully, but doing below
maxLength="getText('abc.def.userID.length')"
is not helping.
Any suggestion is appreciated.

Solved it by using.
<s:set var="userIdLen"><%=getText('abc.def.userID.length')%></s:set>
and then, used it using OGNL in maxLength attribute like below.
<s:textfield maxlength="%{#userIdLen}" key="useralias" autoComplete="off"/>

Related

How do I add a long text field to a custom widget on sitefinity?

I have created a custom widget in Sitefinity, and I am trying to figure out how I can specify a string to as a long text field. How do I do this? I am using MVC and the latest version of Sitefinity.
This is how the field is specified in my controller:
public string Text { get; set;}
The field type is correct. You have to modify the designer view and either use <textarea /> or <sf-html-field /> for rich text capability.
For example:
<div class="form-group">
<label for="description-text">#Html.Resource("Text")</label>
<textarea id="description-text"
class="form-control"
rows="3"
ng-model="properties.Description.PropertyValue">
</textarea>
#*<sf-html-field id="description-text"
class="kendo-content-block"
sf-model="properties.Description.PropertyValue">
</sf-html-field>*#
</div>
Please note that if you want to use <sf-html-field />, you will want to define it in the components section in DesignerView.json file
If you need a thorough understanding of how Sitefinity is creating its OOTB MVC widgets, have a look at https://github.com/Sitefinity/feather-widgets

difference in input type=Checkbox, #HTML.CheckBox and #HTML.CheckBoxFor?

I m new to MVC and confused what is difference in <Input type="Checkbox">, #HTML.CheckBox and #HTML.CheckBoxFor.
Can you please guide why two helpers are provided for same thing ? In which situation which one should be used ?
Thanks
Edit:
Added Input type=checkbox
<Input type="Checkbox"> is Html markup for a checkbox and #Html.CheckBox & #HTML.CheckBoxFor are Html Helpers for Razor view engine..
suppose your viewmodel has a property Person.HadDinner, then usually for model binding to work properly you will have to name the checkbox Person.HadDinner and id as Person_HadDinner..
you can use #Html.CheckBox like
#HTML.CheckBox("Person.HadDinner", Model.Person.HadDinner)
but if you are using #HTML.CheckBoxFor, it will be strongly typed..
#HTML.CheckBoxFor(x => x.Person.HadDinner)
in both the cases, final output markup will be
<input type="checkbox" id="Person_HadDinner" name="Person.HadDinner">
The CheckboxFor (MSDN)
Returns a check box input element for each property in the object that
is represented by an expression.
This means a checkbox element is created for each property in the expression provided. Where as Checkbox (MSDN)
Returns a check box input element by using the specified HTML helper
and the name of the form field.
This creates a simple Checkbox element with the (optional) attributes provided.
Typically when referencing a property of an object (or the View Model) the most desired technique is to use CheckboxFor as the checkbox will be formatted correctly against your model.
Hope this helps.
EDIT: Response to OP Changes.
Both the CheckboxFor and Checkbox generate standard HTML output such as below.
#Html.CheckboxFor(m => m.SomeProperty)
<input type="checkbox" name="SomeProperty" id="SomeProperty" />
#Html.Checkbox("SomeProperty")
<input type="checkbox" name="SomeProperty" id="SomeProperty" />
The helper methods simply generate the HTML required to meet the expressions and attributes defined in the helpers.
Additionally, you dont have to use the helpers. You can write your HTML elements directly as needed.

Inconsistent null attribute handling in ASP.NET MVC 4

I have been trying to handle optional HTML required and readonly attributes in ASP.NET MVC 4. For my surprise, I found out that null attributes in HTML helpers are rendered as empty strings while they are removed completely in Razor (desired behavior).
For example, this code:
#{ string disabled = null; string #readonly = null; }
#Html.TextBox("t1", "Value", new { disabled, #readonly })
<input type="text" name="t2" value="Value" disabled="#disabled" readonly="#(#readonly)" />
Renders:
<input disabled="" id="t1" name="Txt1" readonly="" type="text" value="Value" />
<input type="text" name="t2" value="Value" />
Basically what I want to know is:
What is the reason behind these two different behaviors?
Is there a way to get the same result using Html.TexBox without writing any custom code?
EDIT
This is not possible without writing a custom Html Helper, but there's a feature request for this on CodePlex.
The Html.TextBox() behavior comes from code in System.Web.Mvc.Html that transforms a RouteValueDictionary of attributes into actual HTML. (I believe that code is in TagBuilder)
The raw HTML tag behavior comes from a feature in the Razor v2 language parser that removes attributes in Razor markup that resolve to null at runtime.

struts 2 assigning value of property tag to hidden field

I want to assign the value from field Description to a hidden field test.
But the problem is the "Description" contains sequence of words and the following code is assigning only first word to "test"
<s:hidden value=<s:property value="Description" /> name="test">
I am kind of new to struts. Can someone please help.
Also it would be nice if i get to know good tutorial links of struts2.
If this is a property in your action class you need not to use <s:property value="Description" /> as the Description will be available at the top of value stack and you can use OGNL to fetch the value from value-stack.This is what you need to do
<s:hidden value="%{description}" name="test" />
Please make sure the value in hidden filed should be similar to the name of property in your action class as it will be resolved to either the getter and setter in your action class or the public property defined in your action.
So this means value="%{description}" will be converted by OGNL like getDescription() and will try to find the getter in your action class to fetch the property value.
<s:hidden value="%{description}" name="test" />

show label of an object property in struts2 text tag

I want to show a property of an object in my Jsp page. the problem is that this property is the name of a label in my resource bundle and the only way I know is to use <s:text name="labelname"> but now that it's a property of an object, I don't know how to do this. This is a part of my code, 'brandList' is a list of brand objects and 'brandName' field is name of label. I want something like this
<s:iterator value="brandList" status="stat">
<s:text name="<s:property value='brandName'>" />
</s:iterator>
but this doesn't work. any idea?
Thanks in advance.
Try this:
<s:iterator value="brandList" status="stat" var="brandName">
<s:text name="%{brandName}" />
</s:iterator>

Resources