Struts 2 <s:if> tag issue - struts2

I have a pojo class as below:
public class HelpBean {
private String title;
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
}
An action class as follows:
public class MyAction implements Action {
private HelpBean bean;
//other fields
public HelpBean getBean() {
return bean;
}
public void setBean(HelpBean bean) {
this.bean = bean;
}
}
In my jsp page, I am trying to check for below condition:
<s:if test=%{!bean.getTitle().trim().contains("bad_title")}>
//execute good code
</s:if>
I also tried below, but this one also doesn't work:
<s:if test=%{bean.getTitle().trim().contains("bad_title") == false}>
//execute good code
</s:if>
Not sharing the xml, as routing and everything works fine. I only have problem checking if my title does not contains "bad_title".
There are many different titles so I can't compare for contains() here, hence checking for !contains()
What am I missing here? Can someone pls point out. Before down voting, please explain if something is missed.

IMO the check should be
<s:if test="%{!bean.getTitle().trim().contains('bad_title')}">

Related

struts 2 checkboxlist doesn't save values

jsp content:
<s:checkboxlist list="list" name="values"/>
action content:
public List<Foo> getList() {
return list;
}
public String[] getValues() {
return values;
}
public void setValues(String[] values) {
this.values = values;
}
class Foo:
private String code;
public String getCode() {
return code;
}
public String toString() {
return code;
}
When I put breakpoint at getValues() method, I clearly see it's being called with some values there. But that values don't appear to be selected on a page.
What am I missing here?
I found a solution. I've added
<s:checkboxlist list="list" name="values" listKey="code" listValue="code />
to jsp and it all started working after that. It generates the same html, but it seems that despite rendering correctly, that properties are required by struts to check what values should be set. And there is no emphasis on that in struts docs.
EDIT:
It seems that only this is actually requited for this thing to work:
<s:checkboxlist list="list" name="values" listKey="code"/>

Conversion Error setting value '948' for 'null Converter'

I want to pass some IDs in a URL that are later going to be read by a backing bean. For each ID a particular operation will follow.
F.g. URL: localhost:8585/monit/accepted.jsf?acceptMsg=948&acceptMsg=764
The bean:
#ManagedBean
#RequestScoped
public class AcceptedBean {
#EJB
private MessageService messageService;
#ManagedProperty("#{paramValues.acceptMsg}")
private String[] acceptMsg;
public String[] getAcceptMsg() {
return acceptMsg;
}
public List<Message> getMessages() {
return messageService.getAcceptedMessages();
}
public void setAcceptMsg(String[] acceptMsgs) {
this.acceptMsg = acceptMsgs;
if(acceptMsg != null) {
try {
for (String accept: acceptMsg) {
final Message o = messageService.find(Integer.parseInt(accept));
messageService.accept(o);
FacesContext.getCurrentInstance().
addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Message was accepted:", o.getSubject()));
}
} catch (Exception e) {
FacesContext.getCurrentInstance().
addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Error:", e.getMessage()));
}
}
}
}
acceped.xhtml:
....
<f:metadata>
<f:viewParam name="acceptMsg" value="#{acceptedBean.acceptMsg}" />
</f:metadata>
....
When I tried replacing value with binding in the .xhtml I got
Cannot convert javax.faces.component.UIViewParameter#ee2549 of type class javax.faces.component.UIViewParameter to class [Ljava.lang.String;
Any ideas on how to solve the conversion error would be appreciated!
The <f:viewParam> doesn't support multiple parameter values. Remove it. You don't need it in this particular case. You already have a #ManagedProperty("#{paramValues.acceptMsg}") which already sets them.
See also:
f:viewParam with multiple values
Unrelated to the concrete problem, I'd only replace the business job in the setter by a #PostConstruct method. The getters/setters should in principle not be doing anything else than getting or setting a property, that would indicate bad design as they are merely access points and can be invoked multiple times on a per-request basis.

Struts OGNL if statement related to action class's variable doesn't work,

The problem is:
Inside my action class, i have one variable:
private String commentAdd = "yes";
And the action class goes to reslut.jsp, inside reslut.jsp i have:
<s:set name="allowAddComment" value="commentAdd"/>
<s:if test="%{#allowAddComment=='yes'}">
<script type="text/javascript">
window.close();
</script>
</s:if>
but it doesn't work, can some expert give me some suggestion? Thanks.
A few things.
The property needs to be exposed via a public getter (or in later versions of S2 as a public member, but better to use a getter).
Why use a string as a boolean? Just use a boolean.
Why set the property to a different variable? Just use the property.
Are you sure this is really what you want? This will close the window as soon as that JavaScript is rendered. If that's okay, fine--although if that's the case, why bother rendering the window at all?
import com.opensymphony.xwork2.ActionSupport;
public class PageAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private boolean addComment;
public boolean isAddComment() {
return addComment;
}
public void setAddComment(boolean addComment) {
this.addComment = addComment;
}
public String execute() {
return SUCCESS;
}
}
<s:if test="%{addComment}">
<script type="text/javascript">
window.close();
</script>
</s:if>

JSF 2.0 + Primefaces richtext editor

<p:editor value="#{editorBean.value}" widgetVar="editor" width="686"
height="390" language="en" align="center">
</p:editor>
Following is my rich-text editor bean picked up from primefaces
#ManagedBean(name = "editorBean")
#SessionScoped
public class EditorBean {
private static final String MANAGED_BEAN_NAME = "editorBean";
private String value;
public static EditorBean getCurrentInstance() {
return (EditorBean) FacesContext.getCurrentInstance()
.getExternalContext().getRequestMap().get(MANAGED_BEAN_NAME);
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
Apart from this I have another bean say A. I have a method inside A that populates a HTML table. What I want is when the user opens the editor, it should be pre-populated with that HTML table data and of course the changes should get reflected into (String: value). Therefore, you can say that I am trying to tie up both the values together. I think it needs to be done with DI but somehow its not working. If someone can guide or quote an example, it would be really helpful.
One way to do it is rewrite your getValue() method to pick up the value from bean A.
And yes, the reference to your A bean should come from DI:
//injecting a reference to A
#ManagedPropery(value="#{A}") //or whatever is the name of your bean
private A beanA;
public void setBeanA(A beanA) {
this.beanA = beanA;
}
Or, with CDI, just:
#Inject private A beanA
Finally, your getValue method
public String getValue() {
return beanA.getValue()
}

How To Prepopulate Checkboxes With Struts2 and Jquery?

I am trying to determine the best/easiest way to prepopulate certain checkboxes created using Struts2 form tags. My application is a "normal" three tier setup, using Struts2 on the controller layer.
Before I really, really dig deep here, does the tag support creating the list of all possible checkboxes, then populating it (say, via the below action)?
Sample action:
public class UserManagementAction extends ActionSupport implements Preparable {
private List<String> allRoles;
private List<String> rolesToPrepopulate;
// get/set methods
public void prepare() throws Exception {
// populate the allRoles and rolesToPrepopulate lists
}
public String execute() throws Exception {
return INPUT;
}
(Note: assume that struts.xml has been configured with which JSP to return for INPUT)
Thanks for any help.
Jason
What I would do is a new object class and use it as for checkboxes.
For example:
public class StrutsCheckbox {
private Integer id;
private Boolean selected;
...
}
And in prepare() method you can set selected field as you wish (and also id to all of them).
Next in JSP:
<s:iterator value="allRoles">
<s:checkbox name="selected" id="selected" fieldValue="%{id}" value="%{selected}"/>
</s:iterator>
And then in submit action Collection selected will be filled with ids.
public class UserManagementAction extends ActionSupport implements Preparable {
private List<StrutsCheckbox> allRoles;
private List<StrutsCheckbox> rolesToPrepopulate;
private List<Integer> selectedCheckboxes;
// get/set methods
public void prepare() throws Exception {
// populate the allRoles and rolesToPrepopulate lists
// fill and set allRoles and/or rolesToPrepopulate
}
public String execute() throws Exception {
return INPUT;
}
public String submit() throws Exception {
// list selectedCheckboxes is filled with selected fields id's
return INPUT;
}
Maybe with some corrections it will work, but the main idea is here.

Resources