I would like to know how the default value can be set in textarea , the scenario would be after a page refresh , or fail of validation bedore save operation.
<s:textarea name="desc" value="" theme="xhtml" required="true" cssClass="text textarea small"/>
value="<%= "Default" %>" , this code is not working out.
Perhaps I'm missing something, but IMO this is the same as for any field: the value attribute of a Struts2 tag looks up the respective property in your stack. In the typical scenario, when you type, say, <s:textarea value="comment" ..> Struts2 will use the MyAction.getComment() and MyAction.setComment() to read/write the textarea value. Then, you just have to assign a default value for the attribute in your action -which, BTW, is conceptually the right way.
public class MyAction extends ActionSupport {
public final static String DEFAULT_COMMENT = "Default value...";
private String comment = DEFAULT_COMMENT;
//... getters setters follow
}
Well one of the way would be use Javascript function to load Default value on pageload event... Though I am not sure why your tag is not working
Try to initialize the object linked to the field in the previous action method.
In your java File:
X object = new X() ;
object.setDesc("");
request.setAttribute("theFormObject",object);
In your JSP:
<s:textarea name="theFormObject" property="desc" ... />
Related
I am new to struts2 and looking at the existing code and elsewhere in the net, I thought my below code should work. I am trying to select some user ids in s:select box in JSP and I want to use these ids in my action class. I am using modelDriven for it and users are in a list
JSP code snippet
<s:select id="selectedAgents"
name="selectedUserList"
multiple="true"
list="selectedUserList"
/>
the selected user ids in the page comes to this box. and when the form is submitted, I expect to see the selectUserList in action.
In the action I have
public class WorkLoadReportAction extends GenericAction implements ModelDriven<WorkloadReportDTO>
...
private WorkloadReportDTO userReportInputData = new WorkloadReportDTO();
...
#Override
public WorkloadReportDTO getModel() {
return userReportInputData;
}
the WorkloadReportDTO has List<String> selectedUserList and its getter and setter.
Now in the method of the action(called from submit), I dont see the selectedUserList populated.
What am I missing?
In your Jsp :
<s:select id="selectedAgents"
name="selectedUserList"
multiple="true"
list="selectedUserList"
/>
You have name attribute value and list attribute value as same . Try changing the value of either name attribute or list attribute .
Hope this Solves your problem
I would like to know if there is a way to set a default value to a inputText, or inputHidden or any other tag that can be recovered in the backing bean when the page shows.
I would like something like this (this code doesn't work):
<h:inputText id="companyName" value="#{loginController.companyName}" defaultValue="123456">
And in the backing bean:
private String companyName;
#PostConstruct
public void init() {
System.out.println("CompanyName=" + companyName);
}
So that it shows "CompanyName=123456" in the console.
I need to define the default value in the page itself, I don't want to put the default value in the backing bean.
The real problem is that I need to find a way to pass a value to the backing bean defined in the page. I have many pages and I want to define a 'mode' in each page to be shown differently, and this 'mode' needs to be read by the backing bean when the page displays (before any submit)
I've somewhat solved the problem by showing what I want to show in certain mode, using an h:panelGroup and an EL expression to set the value to the bean:
<h:panelGroup rendered="#{loginController.companyName('123456')}" >
...
</h:panelGroup>
Thanks everybody for your comments
I am using the following code to have different submit buttons for my form. The problem is that if I change the value of the Value attribute of each of the submit buttons I have to change the Java code too as if conditions are based on these values.
I am looking for an alternative solution to avoid this problem, so back-end and front-end would be independent.
JSP
<s:form name="myform" method="POST" action="myformActions">
.....
<input id="sub1Btn" type="submit" name="action" value="mysubmit1"/>
<input id="sub2Btn" type="submit" name="action" value="mysubmit2"/>
</s:form>
Java
{
....
private String action;
....
public void myformActions(){
if(action.equalsIgnoreCase("mysubmit1") //if I change the value need to change this as well
{
do whatever is required to fulfill the request of mysubmit1 ...
}
if(action.equalsIgnoreCase("mysubmit2") //if I change the value need to change this as well
{
do whatever is required to fulfill the request of mysubmit2 ...
}
}
}
I didn't go through all the comments, but from your question , I have an idea like you can use some javascript functions to set the action for the form depending on the criteria you want.So when the form is submitted , corresponding action will be invoked. You can even use a single action class and different methods like edit , add etc , which you can map in the struts.xml file. In this case mapping can be done like action="*user" method="{1}" so the method edit() in the action class will be invoked for action editUser and delete() for deleteUser ...
This solution may not be good in all cases and will not directly answer the question. My solution depends on the fundamental of your actions. If your button defines different actions on a object, you can do below.
This will not move buttons out of your action, but put them in the place they should be.
Consider a page with a form, the user can complete the form and submit it to result page. The result page will have export buttons to let user save the form in html,pdf,excel formats.
Your jsp
<s:submit button="true" key="form.btn.export.pdf" name="export" />
<s:submit button="true" key="form.btn.export.excel" name="export"/>
<s:submit button="true" key="form.btn.export.html" name="export" />
Then in your action the export setter will be if/else or switch statements (not a simple setter):
public void setExport(String exportBtn) {
if (exportBtn.toUpperCase().contains("PDF")) {
this.export = "PDF";
} else if (exportBtn.toUpperCase().contains("EXCEL")) {
this.export = "XLSX";
} else if (exportBtn.toUpperCase().contains("CVS")) {
this.export = "CVS";
} else if (exportBtn.toUpperCase().contains("HTML")) {
this.export = "HTML";
}
LOG.debug("Exporting to is " + this.export);
}
Then in your action doesn't need any if/else.
#Action(value = "export-action")
public String exportMethod(){
ExportHelper ex= new ExportHelper("report",export);//No if is required
inputStream = new ByteArrayInputStream( ExportHelper);
}
This easy solution make your actions more easy to maintenance.
Hint.
In this solution the backend and front end are not coupled! We are coupling the front end(jsp) to Controller(Struts Action). Which is not a bad approach.
use method or action attributes of the submit tag to redirect each to different method / action.
I have this value ${agact.ppr} that i want to show in my page jsp. It can be shown like this easly:
<c:out value="${agact.ppr}" />
but I want to use it into input form like this:
<form:input type="text" value="${agact.ppr}" path="ppr" />
but it's not working I don't know why. I displayed others attributes in input fields without problem and this one no.
My class is like this :
Class Agent{
int ppr;
/** setters and getters**/
}
Try removing the 'value' attribute, 'path' should be all you need there. Also make sure the getter and setter are valid.
I need to determine the ID of a form field from within an action handler. The field is a part of a included facelets component and so the form will vary.
included.xhtml
<ui:component>
<h:inputText id="contained_field"/>
<h:commandButton actionListener="#{backingBean.update}" value="Submit"/>
</ui:component>
example_containing.xhtml
<h:form id="containing_form">
<ui:include src="/included.xhtml"/>
</h:form>
How may I determine the ID of the form in the update method at runtime? Or better yet, the ID of the input field directly.
Bind the button to your backing bean, then use getParent() until you find the nearest form.
Programmatically I would use jsight's method. You can know the id of your elements (unless you let JSF create them, I don't know the means for numbering in the ids) by looking at it. h:form is a naming container so as long as you don't have it wrapped in another naming container it will be containingForm:containedfield The ':' is the naming separator by default is JSF and the ids are created like this, roughly anyway, (parentNamingContainerId:)*componentId
Since update method is of type actionListener, you can access your UI component as follows
public void update(javax.faces.event.ActionEvent ac) {
javax.faces.component.UIComponent myCommand = ac.getComponent( );
String id = myCommand.getId(); // get the id of the firing component
..... your code .........
}