Migrating DynaActionForm to Struts2 - struts2

While migrating from Struts1 to Struts2, I have encountered DynaActionForm. Please provide code snippet as example which will help convert to Struts2.
Tried lot of google search but no luck!

There's no such DynaActionForm in Struts2. If you want to set properties dynamically then use a Map.
Map<String, Object> dynaActionForm;
//getter and setter here
...
dynaActionForm.put("property1", object1.getProperty1());
dynaActionForm.put("property2", object2.getProperty1());
In the JSP you can access this properies
<s:property value="dynaActionForm.property1"/>
<s:property value="dynaActionForm.property2"/>

Related

How to pass bean from action class to other jsp page?

It is very simple to do in servlet using request.setAttribute(). But how to do this in Struts2?
How to use that bean in other jsp page?
Please give example code.
As #AndreaLigios mentions in the comments, you'll want to generate getters and setters in your action classes.
In your JSP files, you'll be able to get them by using the <s:property> tag.

using <set> tag in grails

I am new to grails. I came across a set tag which we can use in gsp pages itself to set the values which is similar to setting model from the controller.
<g:set var="home" value="something" />
so that when we write ${home} it outputs "something".
Is there any way to set the value in sessions in gsp pages itself and not from controller using set tag?
Yes you can do it in gsp pages as well. You just have to include an extra attribute scope to indicate which scopes(session, flash, page and request) you are setting the value to.
<g:set var="home" value="something" scope="session" />
If you do not include the scope option then it defaults to page.
To display the the value you just have to write ${session.home} or ${request.home} or simply ${home} for request scope. Hope this helps.
For more : https://grails.github.io/grails-doc/3.0.x/ref/Tags/set.html
Well! above answer suffices the need. Just wanted to add one more thing that gsp pages comprises of jsp internally and hence all the 9 implict objects are available on gsp pages as well.
request HttpServletRequest object
response HttpServletResponse object
out PrintWriter object used to send output to the client.
session HttpSession object
application ServletContext object associated with application context.
config ServletConfig object associated with the page.
pageContext server-specific features JspWriters.
page synonym for this
Exception handling exceptions and error page redirects.An instance of javax.servlet.jsp.JspException
You could at any point of time access these in your gsp pages.
More you can read from this.
Hope it helps!

how to load page from hyperlink in JSF-2.2 [duplicate]

I've read how to send parameters using JSF but what if the user types their companyId in the URL when accessing their login page? For example,
http://my.company.url/productName/login.faces?companyId=acme.
The way we do it now, there is a bit of scriptlet code that grabs the value from the request and then set it in the session. That parameter changes their look and feel starting from the login page forward so each customer could have a different login page view. We are using extjs until I switch over to JSF.
Is there a way to do that using JSF 2 or perhaps PrimeFaces?
Yes, you can use the <f:viewParam> to set a request parameter as a managed bean property.
<f:metadata>
<f:viewParam name="companyId" value="#{bean.companyId}" />
</f:metadata>
You can if necessary invoke a bean action using <f:viewAction> (JSF 2.2+ only) or <f:event type="preRenderView">.
<f:metadata>
<f:viewParam name="companyId" value="#{bean.companyId}" />
<f:viewAction action="#{bean.onload}" />
</f:metadata>
When using <f:viewAction> you can even return a navigation outcome.
public String onload() {
// ...
return "somepage";
}
When not on JSF 2.2 yet, you can use ExternalContext#redirect() for that. See also among others How to perform navigation in preRenderView listener method.
Note that this is not specific to PrimeFaces. It's just part of standard JSF. PrimeFaces is merely a component library which provides enhanced ajax and skinnability support.
See also:
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
Communication in JSF 2.0 - Processing GET request parameters
#ManagedProperty with request parameter not set in a #Named bean
url paramters can also be treated as request parameters so you can also access through
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
There is a utility library, OmniFaces which does this out of the box.
#Inject #Param
private String key;
#Inject #Param
private Long id;
You can use the request.getQueryString() if you want to get full query parameter string.

struts2 ognl retrieve data from session (nested property tags?)

I have a situation where I would like to retrieve data from the session on a jsp using OGNL. The data in my session is stored like this:
/data/abc/-Name (key) -> ABC Inc. (value)
I can easily retrieve this from the session by doing
<s:property value="#session['/data/abc/-Name']"/>
But unfortunately, the string '/data/abc/' is a dynamic one and is stored in my action under the variable companyFolder.
How do I use this variable to get the data from the session.. something like
<s:property value="#session['%{companyFolder}-Name']"/> // this didnt work
<s:property value="#session['<s:property value="%{companyFolder}"/>-Name']"/> // this didnt work
<s:property value="#session[companyFolder + '-Name']" />
I would likely do this in the action, though, using SessionAware. This makes things easier to test, and avoids executing the JSP to see if things work.

<c:if> test="" in jsf

I'm using primefaces and jstl to loop a datatable.I have a List in backing bean for the columns.
private List<String> visableCols;
public initCols(){
visableCols.add("andOr");
visableCols.add("operator");
......
}
// getter & setter
In the xhtml page.
<p:comlumns var="col" value="#{theBean.visableCols}" >
<c:if test="#{col == 'andOr'}">
<!-- do sth here -->
</c:if>
</p:comumns>
but I found the c:if always false.I tried to print out the #{col} and compare w/ 'andOr',they are the same value.
If you are using PrimeFaces and you want to dynamically add and remove columns, PrimeFaces provides a specific way of doing that. You don't need any JSTL tags. For a good example of how to do it just look at their showcase example. It is pretty involved but fairly clean.
Note: you use the p:columns tag instead of p:column.

Resources