Tapestry: How to set HTML checkbox from java page - java

I am using plain HTML checkbox(not Tapestry type). I need to set the checkbox to checked in my java page. How do I do that?
Here is my tml code fragment
<input type="checkbox" name="leaf" id="leaf" value="leaf"/>
Any help would be appreciated. Thanks.

You need to set the checked property. I'd probably use the <t:any> component.
TML
<t:any element="input" type="literal:checkbox" name="literal:leaf" id="prop:clientId" value="prop:currentObject.value" checked="prop:checked" />
JAVA
#Property
private SomeType currentObject;
public String getClientId() {
return "mycheckbox_" + currentObject.getId();
}
// if this returns null, tapestry won't render the attribute
public String getChecked() {
return currentObject.isSelected() ? "checked" : null;
}

Related

Accessing Script Template Data From Model With Magnolia Java

I need to access the data being input through the Magnolia script template, from the Magnolia Java model.
I have tried setting up parameters and definitions. I have tried using different properties of the definition and content Node.
public FormModel(Node content, ConfiguredTemplateDefinition definition, RenderingModel<?> parent) {
super(content, definition, parent);
}
public String execute() {
HttpServletRequest request = MgnlContext.getWebContext().getRequest();
if ("POST".equals(request.getMethod())) {
try {
//Access the name input from the template script
}
catch(Exception e) {
System.out.print(e);
}
}
return super.execute();
}
}
<form method="post">
<div>
<label>Name:</label>
<input type="text" name="name"/>
</div>
</form>
I want to be able to access the value of input in the Java code.
You need to specify the modelClass attribute in your template definition, and FormModel should extend info.magnolia.rendering.model.RenderingModelImpl<>. You didn't specify if you did any of these, so it's very hard to guess what might be wrong there.
If you're using YAML, the definition should look like this:
renderType: freemarker
templateScript: /my-module/templates/components/home.ftl
modelClass: my.awesome.FormModel

How do I translate text in .java source files?

I have an enum like this
public enum CheckboxFeature {
Option1(" choose this"),
Option2(" or this"),
Option3(" maybe this"),
Option4(" lastly this");
#Getter
private final String text;
public static CheckboxFeature fromName(String v) {
for (CheckboxFeature c: CheckboxFeature.values()) {
if (c.name().equalsIgnoreCase(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
This shows the four options as checkboxes in the web view
<form:checkboxes items="${features}" path="enabledFeatures" itemLabel="text" delimiter="<br/>"/>
How can I translate these options? I use fmt:message for the rest of the translations in the web view.
I have tried
Option1(" <fmt:message key=\"text.test\"/>"),
and
Option1(" ${option1}"),
with
<fmt:message key="text.select" var="option1"/>
in the .jsp.
Neither of them work, so it seems it can't work this way. What is the correct way of translating the strings? Ideally using the fmt:message and i18n resources (lang.properties files) that are in place and working on the rest of the servlet?
Optimally, you get the resource key from the enum, and look that up.
public enum CheckboxFeature {
Option1("label.option1.key"),
Option2("label.option2.key"),
Option1("label.option3.key"),
Option2("label.option4.key");
private final String key;
[...]
I don't know how to nest a l10n lookup in the itemLabel attribute, so I would write something like:
<c:forEach items="Options.values()" var="current">
<form:checkbox path="selectedOptions" value="${current.name()}"/> <fmt:message key="${current.getKey()}"/>
</c:forEach>
1、CheckboxFeature add method like:
public static List<String> getAllCheckboxFeature(String v) {
return Arrays.asList(Option1.getText(),...Option4.getText());
}
2、than use the jstl like:
<c:forEach items="${options}" var="option">
<input type="checkbox" ="${option}">
</c:forEach>

how to add values in second dropdownchoice when i select value in first dropdownchoice?

i have two dropdownchoice,After selecting value from first dropdownchoice i need to
select list of related values from database table and add this list into second
dropdownchoice. Here is my sample code for reference.
Give me any hint or reference.
HTML:
<select wicket:id="DDCStartDate" style=" width : 98px;"></select>
<select wicket:id="DDCEndDate" style=" width : 98px;">.
Java code:
final DropDownChoice DDCStartDate=new DropDownChoice("DDCStartDate",new
PropertyModel(collectionReportModel, "DDCStartDate"),lst);
add(DDCStartDate);
DDCStartDate.setOutputMarkupId(true);
final DropDownChoice DDCEndDate=new DropDownChoice("DDCEndDate",);
add(DDCEndDate);
DDCEndDate.setOutputMarkupId(true);
POJO code:
private Date DDCStartDate;
private Date DDCEndDate;
public Date getDDCStartDate()
{
return DDCStartDate;
}
public void setDDCStartDate(Date dDCStartDate)
{
DDCStartDate = dDCStartDate;
}
public Date getDDCEndDate() {
return DDCEndDate;
}
public void setDDCEndDate(Date dDCEndDate) {
DDCEndDate = dDCEndDate;
}
You can do this using an AJAX callback. Check the "Drop Down Choice Example" on Wicket Examples.
http://www.wicket-library.com/wicket-examples/ajax/
This is the important file that shows you how to do this using the correct models.

How to make spring checkboxes checked by default?

I'm using Spring 3.1.0.RELEASE. I have this field in my command object ...
public Set<EventFeed> getUserEventFeeds() {
return this.userEventFeeds;
}
On my Spring JSP page, I want to display a checkbox list of all possible event feeds, and then have checked checkboxes if the user has one in his set. I want to have some special HTML formatting around each checkbox, so I'm trying ...
<form:form method="Post" action="eventfeeds.jsp" commandName="user">
...
<c:forEach var="eventFeed" items="${eventFeeds}">
<tr>
<td><form:checkbox path="userEventFeeds" value="${eventFeed}"/></td>
<td>${eventFeed.title}</td>
</tr>
</c:forEach>
...
However, the items aren't getting checked by default if one is in the set. How do I do this? Here is the binder I'm using in my controller class ...
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(EventFeed.class, new EventFeedEditor());
}
private class EventFeedEditor extends PropertyEditorSupport {
#Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(eventFeedsDao.findById(Integer.valueOf(text)));
}
#Override
public String getAsText() {
return ((EventFeed) getValue()).getId().toString();
}
}
#Dave, there is something called form:checkBoxes. You can try with that.
<form:checkboxes path="userEventFeeds" items="${eventFeeds}" itemLabel="id" itemValue="value"/>
My assumption here is you should have "id" and "value" defined in the EventFeed class.
I just tried this by having a String[] availableList and String[] selectedList. It works like charm. You can give a try as well.
Interestingly this works:
<form:checkbox path="services" value="${type}" checked="checked"/>
You can do this by placing selected default property true in your class
class User {
boolean userEventFeeds = true;
}
I've tried both form:checkboxes and form:checkbox with the same data and the first works, the second doesn't. (Same release of Spring you have)
It looks like there was a bug which, despite their claim, seems to be still there.
For my usecase (reacting on stuff in a list that has nothing to do with the object being filled), this code worked:
(Please be aware that this is a last-resort kind of code, other solutions are most likely better suited.)
<%# taglib prefix="jstl" uri="http://java.sun.com/jsp/jstl/core" %>
<jstl:forEach var="listObject" items="${someList}">
<jstl:if test="${listObject.active}">
<form:checkbox path="active" checked="checked"/>
</jstl:if>
<jstl:if test="${!listObject.active}">
<form:checkbox path="active"/>
</jstl:if>
</jstl:forEach>

Multiple Submit Buttons problem in Struts2

Trying to work with multiple submit buttons within a single form in struts2 application but not able to work.
here is the jsp code i am using
<tr>
<td class="button"><input type="submit" value="Import"
name="destinationImport" class="button"></td>
<td class="button"><input type="submit" value="Export"
name="destinationExport" class="button"></td>
</tr>
here is the java part
private boolean destinationImport;
private boolean destinationExport;
//and the respective setters and getters
but i am sure is that Struts2 type convertor is having problem converting the String value to boolean
do any one have idea how to achieve this
Thanks in advance
Methods : getDestinationExport / setDestinationExport should deal with String, since your values: "Export" and "Import" aren't convertible directly to boolean type.
If you need convert it by internal rule, place corresponding code inside setDestinationExport. Something like that:
public void setDestinationExport(String arg){
destinationExport = "Export".equals(arg);
destinationImport = "Import".equals(arg);
}
This way should works
private boolean destinationImport = false;
private boolean destinationExport = false;
public void setDestinationImport(boolean destinationImport) {
this.destinationImport = true;
}
public void setDestinationExport(boolean destinationExport) {
this.destinationExport = true;
}
Reference:
http://serpensalbus.com/blog/tricking-struts2-multiple-submit-buttons/

Categories