struts 2.2.1 appends ".action" suffix to name - struts2

My project changed version of struts from struts-2.1.8.1 to struts-2.2.1.
We don't use suffix ".action" for naming, after migration it is appeared. For older version html code looks like:
<form id="Login" name="Login" action="/fm2/Login" method="post">
But new struts renders the same form:
<form id="Login" name="Login" action="/fm2/Login.action" method="post"
So difference that .action has been added. What's wrong with new release?

This is the default extension (and should be in 2.1.8.1 too).
You can change it in your struts.xml:
<constant name="struts.action.extension" value="whatever" />

I have a similiar problem when change to struts-2.2.1 from struts-2.1.8.1.
Struts-2.2.1 will add the ".action" extension automatically for redirectAction result.
This is very annoying.

This has not changed, AFAIK. Be sure to understand the difference between the "struts action" and the "action attribute of a HTML FORM" element
Typically, to render a FORM tag in Struts2 you'd use a (Struts2) form tag - its action attribute corresponds to the name of a Struts2 action, which corresponds to a url without the suffix (by default '.action', but you can change it)
So, the Struts2 tag
<s:form action="/fm2/Login">
would typically produce the HTML output
<form action="/fm2/Login.action">

I removed the struts2-convention-plugin-2.1.8.1 jar from my web-inf/lib and it started to work fine.
Hope this helps...
cheers...

Related

Use placeholders with <f:all bean />

In Grails, you can generate a form using <f:all bean="beanName" />. And while the generated form looks great, I was wondering if it would be possible to have a placeholder in the field, so that the result becomes something like:
<input type="text" name="question" placeholder="type your question here" />
I tried using the attributes validation in Grails, like this:
class Question {
static constraints = {
question(size:5..100, attributes:[placeholder:"type your question here"])
}
}
But it doesn't seem to have any effect on the generated HTML.
Just in case there's any confusion, the f:all tag is provided by the fields plugin. I don't think there's any way you can specify the placeholder attribute via the domain class constraints, but there are a few other options.
One option is to define a custom (GSP) template for this property and specify the placeholder attribute therein. The path to this template will depend on which version of the plugin you're using, but you can find the details here.
Alternatively, if you render each field individually with f:field, rather than using f:all you can pass additional attributes to the input field, e.g.
<f:field bean="person" property="gender"
widget-placeholder="type your question here"/>
In earlier versions of the plugin (before 1.5), the attribute should be named input-placeholder instead, e.g.
<f:field bean="person" property="gender"
input-placeholder="type your question here"/>

i18n of URL in a java project

I am trying to internationalise the urls of my web application. Basically I want to be able to set the url of, let's say, a link to www.mysite.com/france/inscription when the preferred language is fr and www.mysite.com/signup when the preferred language is en.
Until now I have beenusing a jtsl choose to manually set the url:
<c:choose>
<c:when test="${pageContext.request.locale.language eq 'fr'}">
<a class="trialButton" href="<c:url value="/france/inscription?type=0"/>"><fmt:message key='features_trial'/></a>
</c:when>
<c:otherwise>
<a class="trialButton" href="<c:url value="/subscribe?type=0"/>"><fmt:message key='features_trial'/></a>
</c:otherwise>
</c:choose>
Ideally I would like to be able to get a localised url directly from my resource bundle, I tried to do:
<a class="trialButton" href="<c:url value=<fmt:message key='features_trial_url'/>/>"><fmt:message key='features_trial'/></a>
But eclipse doesn't accept it.
Do you know a way to do something like that?
You can't use a JSP tag inside an attribute of another JSP tag. But all JSTL tags printing a value have an attribute var allowing to store the value inside an attribute instead of printing it. So you can simply use
<fmt:message key='features_trial_url' var"theUrl"/>
<a class="trialButton" href="<c:url value='${theUrl}'>"><fmt:message key='features_trial'/></a>

Can i use html tags in struts2 form?

Can i use html form tags in struts 2 form?
Like
<input type='text' value='' />
<input type='submit' />
Will the values be posted through struts2?
It's not at all mandatory to use struts2 tags. You could go with regular HTML.
Of course.
This is one of those questions you can just try.
All the S2 form tags do is emit HTML, filling in various attributes as required. (It's slightly more complicated than that, but ultimately, they spit out an HTML form field.)
Flip your question on its head: why wouldn't a hand-crafted input tag be sent via the normal browser HTTP submission process? What mechanism could prevent it from working? How is the request body of from such a form submission different from one where the input tags are S2 custom tags?
These questions are all trivial to explore.
Yes.
You must give them a name; the name will be used to set properties (with correct type conversion) in the struts action.
If you call an input somename the setSomename() will be called on post.
If simple HTML used you wont be able to call struts tags inside it eg:
<s:submit cssStyle="submit_button" id='newrc%{#stat.index}.%{#questionIndex.index}' name="newrc%{#stat.index}.%{#questionIndex.index}" onclick="return newrcClick(this)" value="+" />
This works but below code does not provide values for id and name from values stack thus :name="newrc%{#stat.index}.%{#questionIndex.index}"
<input type="button" cssStyle="submit_button" id='newrc%{#stat.index}.%{#questionIndex.index}' name="newrc%{#stat.index}.%{#questionIndex.index}" onclick="return newrcClick(this)" value="+" />

Struts2 : Using form action

I am working with a struts2 based application
Inside my JSP page for form submission is it mandatory to use s:form (Predefined struts component )
Because when i tried this way it worked (calling the Action class under struts.xml )
<s:form action="HelloWorld" >
<s:submit />
</s:form>
But When I tried to use normal form submission as shown
<form action="HelloWorld">
<input type="Submit"/>
</form>
It isn't working , it gave me 404 error .
please tell me is it mandatory to use and for data submission ??
A struts form action and an HTML tag form action are different. You can use a standard HTML form tag with struts if you create a struts specific URL for example (off the top of my head):
if using in multiple places, generate the url in and call like this -
<s:url id="myActionUrl" action="HelloWorld" />
<form action="<s:property value="%{myActionUrl}" />">
<input type="Submit"/>
</form>
or using in a single instance -
<form action="<s:url id="myActionUrl" action="HelloWorld" />">
<input type="Submit"/>
</form>
You can often look at the page source in your browser to see what Struts generates and recreate it manually like this. You will often end up using additional struts tags such as property to retrieve values from your value stack, but it is useful at times, for instance when generating JavaScript code dynamically.
No, it's not mandatory to use any of the S2 tags, but as Russell says, you need to duplicate the correct action URL.
You also need to be a little careful when mixing-and-matching S2 form tags with non-S2 HTML form tags, because the default S2 theme adds additional HTML markup to the page; they don't just create form tags--the default theme uses table tags to lay out the form.
You can use s:form for form and
<input type="Submit"/>
can be replaced by
<button type="submit"/>
It's not about which to use,it is what you want from it.Struts2 tags provides additional capabilities to the form.
Please go through below two links to get the diffrence
1] http://struts.apache.org/release/2.1.x/docs/form-tags.html
2] http://www.w3schools.com/tags/tag_form.asp
some facilities such as namespace,tooltip,tooltipIconPath and many are provided by struts2 tags.

struts 2 text tag with dynamic expression , how?

I need to supply the key dynamically from the action to the text tag
<s:text name="<%=talkToUsInfo.messageKey%>" />
but the name attribute on text tag is declared as false ( not sure why ? )
How do i get something like this working without changing the tld in the jar file ?
Take a look at OGNL
It might look like this
<s:text name="%{talkToUsInfo.messageKey}" />
Struts documentation says:
Instead, you should use the getText() method that you inherit when your Action extends XWork's ActionSupport:
<s:textfield name="lastName" label="getText('person.lastName')" />
So I used e.g.
<s:property value="getText('status' + #someObject.currentStatus)" />
instead of "s:text" and it worked.
I prefer to use OGNL and s:text and do not make a call to getText manually.
You usually define a fixed prefix while add the dynamic part to it, so some thing like this could be used:
<s:text name="%{'student.types.'+currencyType}" />
The only time which I used getText inside the s:property was something like this (which needs some if/else if you wanted to use s:text ):
<s:property value="shouldCancelIt.equals(\"0\") ?
getText('label.yes'):getText('label.no')" />

Resources