In my Spring Webflow (with JSP views) I have a model that contains an enum for one of its fields. I need a form with a set of radio buttons that contain only a couple of those possible enum values and I can't seem to crack how to do it.
So my flow is something like this:
<on-start>
<evaluate expression="someService.getModel()" result="flowScope.myModel" />
</on-start>
<view-state id="step1" view="step1View" model="myModel">
<transition on="proceed" to="step2"/>
</view-state>
And myModel has a field called "paymentType" which is an enum (called PaymentType) that could look soemthing like this:
public enum PaymentType{
RE("A paper reciept"),
CC("Credit Card"),
PA("Pre-Authorised Debit"),
MC("MyCoin"),
private final String shortDescription;
PaymentOptionType(final String shortDescription) {
this.shortDescription = shortDescription;
}
public String getShortDescription() {
return shortDescription;
}
}
So let's say in my step1 page I only want to put 2 of these values on the form.
So it would look like:
* A paper reciept
* MyCoin
|Submit|
And it would bind the selected value to the paymentType field in the model.
So I'm wondering how to populate the radio buttons in my form in step1.jsp
<form:form id="paymentForm" name="paymentForm" modelAttribute="myModel" method="post" action="${flowExecutionUrl}">
<!-- What to put for radio buttons here? -->
<button name="_eventId_proceed">Next Step</button>
</form:form>
try:
<form:radiobutton path="paymentType" value="RE"/>A paper receipt
<form:radiobutton path="paymentType" value="MC"/>My Coin
..
or add something like this to your flow
<on-start>
<evaluate expression="someService.getPaymentTypes()" result="flowScope.paymentTypes" />
</on-start>
and then use
<c:forEach var="paymentType" items="${paymentTypes}>
<form:radiobutton path="paymentType" value="${paymentType}"/>${paymentType.shortDescription}
</c:forEach>
Related
I have a drop down box via the <sj:select> tag that I can't seem to get to populate. I have a getter in the action that it is referencing via the href but it doesn't seem to trigger that action so it isn't seeing the getter. I'm using this method which should work from what I've seen from a few tutorials but I'm apparently missing something somewhere.
In its current state now it's just rendering and empty drop down box.
Quick rundown of what I think should be happening:
page should load via InputAction, which it does and populates the list variable with a list of 20 strings
the <sj:select> should fire off the action referenced in the href
that should trigger the RateClass action which populates my list box
I am going about it this way because based on the input of this box I will be switching in and out lists of strings for future select boxes in this application.
Action Class:
public class RateClass extends InputAction{
/**
*
*/
private static final long serialVersionUID = -836858635953762820L;
private String selectedUtility;
public String execute(){
return SUCCESS;
}
public String getJSON(){
System.out.println("YAY JSON!!!");
return execute();
}
public List<String> getUtilityList() {
return utilityList;
}
public String getSelectedUtility() {
return selectedUtility;
}
public void setSelectedUtility(String selectedUtility) {
this.selectedUtility = selectedUtility;
}
}
Struts.XML json package:
<package name="jsonPackage" extends="json-default" namespace="/">
<action name="rateclass" class="rateclass">
<result name="success" type="json" />
</action>
</package>
Bean Definition:
<bean id="rateclass" class="com.action.input.RateClass" scope="prototype">
</bean>
Relevant .jsp excerpt:
<label for="utility">Utility: </label>
<sj:select style="margin-left:50px;" href="%{remoteurl}" id="utility"
name="selectedUtility" list="utilityList"
headerKey="-1" headerValue="Please Select a Utility"/>
Struts URL definition:
<s:url id="remoteurl" value="rateclass"></s:url>
The issue was in the prepare() function of my InputAction class that was extended off of my JSON class. Inside of that there was a nullpointer being thrown that wasn't handled due to a DAO object I did not define in my RateClass bean.
I have the following form in Spring:
<form:form method="post" action="addprice.html" commandName="addprice">
<form:input path="name" />
<form:input path="price" />
<input type="submit" value="<spring:message code="Add price"/>"/>
</form:form>
And I need to pass the parameters to the following function:
#RequestMapping("/addprice")
public String addPrice(
// WHAT I NEED TO WRITE HERE?
)
{
// function code
}
What I need to write between parentesys in the previous function in order to retrieve "name" and "price"?
I tried using #ModelAttribute but it seems to work only with entities and I don't have a unique entity in this case but just two simple parameters.
Use the #RequestParam annotation :
#RequestMapping("/addprice")
public String addPrice(#RequestParam String name, #RequestParam double price)
{
// function code
}
I use spring 3. I'm trying to display a value of an object in a jsp.
public class UserForm implements Serializable {
private List<String> values;
private String date;
...
}
Here is my controller:
#Controller
#RequestMapping("/user.htm")
public class UserController {
#Autowired
private IUserService userService;
#RequestMapping(method = RequestMethod.GET)
public String userStat(Model model) {
UserForm stat = userService.populateStatistique();
stat.setDate("today");
model.addAttribute("statistiqueForm", stat);
return "userStat";
}
}
Here is my jsp:
<form:form commandName="userForm" name="userForm">
Date: <form:input path="date"></form:input>
<br/>
Expediteur:
<form:select path="values" items="${values}" />
<br/>
<input type="submit" value="create" />
</form:form>
In the jsp I can see the today value for the date field, but the listbox is empty.
any idea?
thanks
Well, use addAttribute("values", list) if you want it accessible in the jsp. You are currently not setting it and so it is empty.
If that list is contained in the statistiqueForm object, then use items="${statistiqueForm.values}".
You're correctly passing the form object to the jsp page. In that page you have a list box, which has a list of potential values and a list of selected values. The object form contains the list of selected values, but you should also setup the list with all potential values. In fact you reference ${values} in the jsp, but you don't pass it to the jsp. You should add this code to your controller:
model.addAttribute("values", potentialValueList);
I also suggest you to change the name of that list to avoid confusion, so it will be easy to understand the difference from selected values to potential values.
I would like to know how to use Converters in Java Server Faces similar to Spring collection property editor
Suppose the following model
public class Group {
private String name;
List<User> users = new ArrayList<User>();
// getter's and setter's
}
And equivalent form
<form ...>
<h1>Group form</h1>
<label for="name">Enter name</label>
<input type="text" name="name"/>
<label for="users">Select users</label>
<!--value attribute stores userId-->
<input type="checkbox" value="1" name="users"/> User 1
<input type="checkbox" value="2" name="users"/> User 2
<input type="checkbox" value="3" name="users"/> User 3
</form>
If i use Spring to bind users property in Group class, i call
binder.registerCustomEditor(List.class, new CustomCollectionEditor() {
protected Object convertElement(Object userId) {
return new User((Integer) userId);
}
});
How do i get the same effect when using Java Server Faces ?
regards,
For that you can implement javax.faces.convert.Converter. Its API is pretty self-explaining: write the getAsString() method accordingly that it returns the String representation of the Object, which can be under each the technical ID such as userId. Then, to get JSF set the right Object during apply request parameters phase, you need to implement getAsObject() that it returns the Object associated with the given String value.
Basically:
public class UserConverter implements Converter {
private UserDAO userDAO = SomeDAOManager.getUserDAO();
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((User) value).getId());
}
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return userDAO.find(Long.valueOf(value));
}
}
Register it in faces-config.xml as follows:
<converter>
<converter-for-class>com.example.model.User</converter-for-class>
<converter-class>com.example.converter.UserConverter</converter-class>
</converter>
That should be it. For more insights you may this or this article useful.
I have a form where i have to preselect some checkboxes. How is that possible with jsf/seam? In plain html you would just add the "checked" (or checked="checked") as attribute to the checkboxes. But with f:selectItems I have no clue...also the Object "SelectItem" does not provide any setter for this...
You need to preset them in the property behind the component's value attribute as you usually do for every UIInput component. You can do that in the bean's constructor or initialization block.
Here's a basic example:
<h:selectManyCheckbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>
Bean:
private List<String> selectedItems; // +getter +setter.
private List<SelectItem> selectItems; // +getter.
public Bean() {
// Preset the selected items.
this.selectedItems = new ArrayList<String>();
this.selectedItems.add("valueToBePreselected1");
this.selectedItems.add("valueToBePreselected2");
// Those values should be exactly the same as one of the SelectItem values.
// I.e. the Object#equals() must return true for any of them.
}
Populate the property you use in "value" before rendereing the page (for example using a phase listener)
<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
<f:selectItem itemLabel="India" itemValue="India" />
<f:selectItem itemLabel="China" itemValue="China" />
<f:selectItem itemLabel="Germany" itemValue="Germany" />
<f:selectItem itemLabel="USA" itemValue="USA" />
</h:selectManyCheckbox>