Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in dynamic forms

 
Girish Mony
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am new to JSF.
I am doing an application in which this is one of the requirement.

For the new entry page, I have a radio button which shows article and subscriber. The field names and the type of input tags are defined in the database. When the user selects either article or subscriber, the application need to display all the field names and the tags defined for the particular option in the database.

If I add a new field to either article or subscriber in the database, it should also be displayed automatically without any changes in the view code.

Is there any common tag for the input where the type can be changed dynamically at the run time or any other way to solve this problem?

I searched Google, but unable to get a solution to this problem. Please help.

Thanks in advance

Girish
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about JSF, however AJAX & Javascript DOM may help here.
 
Girish Mony
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kiran,

Thank you for the reply. I managed to get the code through the site dynamically adding components

But the problem is It shows error .

This is my jsp code

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<f:view>
<h:form>
<h:selectOneRadio id="category"
value="#{binding.valueOfRadio}"
immediate="true"
onclick="submit()">
<f:selectItem id="article" itemLabel="Article" itemValue="article"/>
<f:selectItem id="subscriber" itemLabel="Subscriber" itemValue="subscriber"/>
<f:valueChangeListener type="#{binding.radioButton}" />
</h:selectOneRadio>
<hutputText value="article" binding="#{binding.articleOutputText}"/>
<hutputText value="subscriber" binding="#{binding.subscriberOutputText}"/>
</h:form>
</f:view>
</body>
</html>

This is my java code

public class binding implements ValueChangeListener
{
private String valueOfRadio;
private String valueOfInput;
UIComponent radioButton = new HtmlSelectOneRadio();
UIComponent articleOutputText = new HtmlOutputText();
UIComponent subscriberOutputText = new HtmlOutputText();
{
articleOutputText.setRendered(false);
subscriberOutputText.setRendered(false);
}

/** Creates a new instance of binding */
public binding()
{

}

public String getValueOfInput() {
return valueOfInput;
}

public void setValueOfInput(String valueOfInput) {
this.valueOfInput = valueOfInput;
}



public String getValueOfRadio()
{
return valueOfRadio;
}

public void setValueOfRadio(String valueOfRadio)
{
this.valueOfRadio = valueOfRadio;
}



public UIComponent getArticleOutputText()
{
return articleOutputText;
}

public void setArticleOutputText(UIComponent articleOutputText) {
this.articleOutputText = articleOutputText;
}

private void encodeInputText(FacesContext context)
{
HtmlInputText inputText = new HtmlInputText();
try
{
inputText.encodeBegin(context);
inputText.encodeEnd(context);
context.renderResponse();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public UIComponent getRadioButton()
{
FacesContext context = FacesContext.getCurrentInstance();
ValueChangeEvent event = new ValueChangeEvent(radioButton, this, this);
Object value = event.getNewValue();
System.out.println("Value "+event.getNewValue());
setValueOfRadio(event.getNewValue().toString());
if(value.equals("article"))
{
articleOutputText.setParent(radioButton);
articleOutputText.setRendered(true);
subscriberOutputText.setRendered(false);
}
else
{
encodeInputText(context);
articleOutputText.setRendered(false);
subscriberOutputText.setRendered(true);
subscriberOutputText.setParent(radioButton);
}
context.renderResponse();
return radioButton;
}

public void setRadioButton(UIComponent radioButton) {
this.radioButton = radioButton;
}



public UIComponent getSubscriberOutputText() {
return subscriberOutputText;
}

public void setSubscriberOutputText(UIComponent subscriberOutputText) {
this.subscriberOutputText = subscriberOutputText;
}

public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
throw new UnsupportedOperationException("Not supported yet.");
}
}

Can you figure it out why?
 
Girish Mony
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry if I have mistaken. Is there any common tag for input in jsf whose type can be dynamically changed during runtime? I heard that struts has one.

Thanks in advance,
Girish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic