开花流水

空山无人,水流花开。

BlogJava 首页 新随笔 联系 聚合 管理
  79 Posts :: 42 Stories :: 160 Comments :: 0 Trackbacks

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<%@ page contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!-- RichFaces tag library declaration -->
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<html>
 <f:view>
  <head>
   <title>JSF in Action - Validator examples</title>
  </head>
  <body>
   <h1>
    Validator examples
   </h1>


   <h:form>
    <table border="1" cellspacing="0" cellpadding="5">
     <tr>
      <td>
       <b>Validator(s)</b>
      </td>
      <td>
       <b>Comments</b>
      </td>
      <td>
       <b>Input</b>
      </td>
      <td>
       <b>Errors</b>
      </td>
     </tr>
     <tr>
      <td>
       Validation method
      </td>
      <td>
       validator=testForm.validateEmail
      </td>
      <td>
       <h:inputText id="methodInput"
        validator="#{testForm.validateEmail}" />
      </td>
      <td>
       <h:message for="methodInput" style="color: red" />
      </td>
     </tr>
     <tr>
      <td>
       None (component property)
      </td>
      <td>
       required="true"
      </td>
      <td>
       <h:selectOneMenu id="RequiredInput" required="true">
        <f:selectItem itemValue="" itemLabel="" />
        <f:selectItem itemValue="1" itemLabel="dogs" />
        <f:selectItem itemValue="2" itemLabel="birds" />
        <f:selectItem itemValue="3" itemLabel="hamsters" />
       </h:selectOneMenu>
      </td>
      <td>
       <h:message for="RequiredInput" style="color: red" />
      </td>
     </tr>
     <tr>
      <td>
       Length
      </td>
      <td>
       minimum="2"
       <br>
       maxmimum="10"
      </td>
      <td>
       <h:inputText id="LengthInput">
        <f:validateLength minimum="2" maximum="10" />
       </h:inputText>
      </td>
      <td>
       <h:message for="LengthInput" style="color: red" />
      </td>
     </tr>
     <tr>
      <td>
       LongRange
      </td>
      <td>
       minimum="5"
       <br>
       maxmimum="999999"
      </td>
      <td>
       <h:inputText id="LongRangeInput">
        <f:validateLongRange minimum="5" maximum="999999" />
       </h:inputText>
      </td>
      <td>
       <h:message for="LongRangeInput" style="color: red" />
      </td>
     </tr>
     <tr>
      <td>
       DoubleRange
      </td>
      <td>
       minimum="5.1"
       <br>
       maxmimum="6.76"
      </td>
      <td>
       <h:selectOneRadio id="DoubleRangeInput">
        <f:selectItem itemValue="5.0" itemLabel="5.0" />
        <f:selectItem itemValue="6.1" itemLabel="6.1" />
        <f:selectItem itemValue="6.8" itemLabel="6.8" />
        <f:validateDoubleRange minimum="5.1" maximum="6.76" />
       </h:selectOneRadio>
      </td>
      <td>
       <h:message for="DoubleRangeInput" style="color: red" />
      </td>
     </tr>
     <tr>
      <td>
       Length, LongRange
      </td>
      <td>
       required="true"
       <br>
       Length minimum="2"
       <br>
       Length maxmimum="3"
       <br>
       LongRange minimum="10"
       <br>
       LongRange maxmimum="999"
      </td>
      <td>
       <h:inputText id="MultiInput" required="true">
        <f:validateLength minimum="2" maximum="3" />
        <f:validateLongRange minimum="10" maximum="999" />
       </h:inputText>
      </td>
      <td>
       <h:message for="MultiInput" style="color: red" />
      </td>
     </tr>
    </table>
    <p>
     <h:commandButton value="Go!" />
   </h:form>
  </body>
 </f:view>


</html>

posted on 2009-06-05 13:57 开花流水 阅读(1017) 评论(2)  编辑  收藏 所属分类: java技术

Feedback

# re: jsf验证的例子 2009-06-05 13:59 空山戊
import java.io.*;
import javax.faces.event.ActionEvent;
import javax.faces.context.*;
import java.util.*;
import javax.faces.component.*;
import javax.faces.application.*;
import javax.faces.model.*;
import javax.faces.component.html.*;
import javax.faces.event.*;
import javax.faces.lifecycle.*;
import javax.faces.FactoryFinder;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.component.UICommand;
import javax.faces.validator.*;

@SuppressWarnings("serial")
public class TestForm implements Serializable
{
private int counter;
private String message = "What time is love?";
private String loggingMessage;
private boolean toggleFlag = false;
private int number;
private String string;
private javax.faces.component.UIData testTable;
private double doubleValue = 0.056;
private javax.faces.model.SelectItem[] colors;
private transient UserConverter userConverter = new UserConverter();
private UIPanel converterPanel = null;
private UserBean user;
private javax.faces.component.UIViewRoot view;
private javax.faces.component.html.HtmlPanelGrid changePanel;
private javax.faces.component.html.HtmlOutputText outputText;
private javax.faces.component.html.HtmlDataTable dataTable;
private javax.faces.application.Application application;
private boolean addMessages = true;

// Actions

public TestForm()
{
colors = new SelectItem[5];
colors[0] = new SelectItem("0", "blue");
colors[1] = new SelectItem("1", "red");
colors[2] = new SelectItem("2", "green");
colors[3] = new SelectItem("3", "black");
colors[4] = new SelectItem("4", "purple");

UIOutput output = (UIOutput)getApplication().createComponent("javax.faces.Output");

LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
lifecycle.addPhaseListener(
new PhaseListener()
{
public void beforePhase(PhaseEvent event)
{
// refreshList();
}

public void afterPhase(PhaseEvent event)
{
}

public PhaseId getPhaseId()
{
return PhaseId.RENDER_RESPONSE;
}
});

}

public UICommand getTestCommand()
{
UICommand command = (UICommand)getApplication().
createComponent(HtmlCommandButton.COMPONENT_TYPE);
command.addActionListener(new ActionListener()
{
public void processAction(ActionEvent event) throws AbortProcessingException
{
FacesContext.getCurrentInstance().getExternalContext().
log("User fired action for command " + event.getComponent());
}
});
command.setValueBinding("value",
getApplication().createValueBinding("#{myBean.userName}"));
command.setValueBinding("title",
getApplication().createValueBinding("#{myBundle.userNameTitle}"));

LongRangeValidator myValidator = (LongRangeValidator)
application.createValidator(LongRangeValidator.VALIDATOR_ID);
myValidator.setMinimum(0);
myValidator.setMaximum(555);
UIInput input = new UIInput();
input.addValidator(getApplication().createValidator(LongRangeValidator.VALIDATOR_ID));
return null;
}

public String toggleNextOrPrevious()
{
toggleFlag = !toggleFlag;
if (toggleFlag)
{
return "next";
}
else
{
return "previous";
}
}

public String incrementCounter()
{
counter++;
return "success";
}

public String loadInbox()
{
return "inbox";
}

public String logout()
{
return "logout";
}

public void incrementCounter(ActionEvent e)
{
counter++;
}

// Properties

public int getCounter()
{
return counter;
}

public void setCounter(int counter)
{
this.counter = counter;
}

public String getMessage()
{
return message;
}

public void setMessage(String message)
{
this.message = message;
}

public String getLoggingMessage()
{
return loggingMessage;
}

public void setLoggingMessage(String loggingMessage)
{
this.loggingMessage = loggingMessage;
}

public boolean getTrueProperty()
{
return true;
}

public boolean getFalseProperty()
{
return false;
}

public int getNumber()
{
return number;
}

public void setNumber(int number)
{
this.number = number;
}

public String getString()
{
return string;
}

public void setString(String string)
{
this.string = string;
}

public void setConverterPanel(UIPanel panel)
{
this.converterPanel = panel;
}

public UIPanel getConverterPanel()
{
if (converterPanel == null)
{
converterPanel = (UIPanel)FacesContext.getCurrentInstance().getApplication().createComponent(HtmlPanelGrid.COMPONENT_TYPE);
UIOutput output = (UIOutput)FacesContext.getCurrentInstance().getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);
output.setValue("foo2");
converterPanel.getChildren().add(output);
}
return converterPanel;
}

// Event handlers

public void toggleLocale(ActionEvent e)
{
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot view = context.getViewRoot();
if (view.getLocale().equals(Locale.ENGLISH))
{
view.setLocale(new Locale("es", "ES"));
}
else
{
view.setLocale(Locale.ENGLISH);
}
}

public void validateEmail(FacesContext context, UIComponent component,
Object value)
{
EditableValueHolder input = (EditableValueHolder)component;
context.getExternalContext().log("inside validateEmail");
if (((String)value).indexOf("@") > -1)
{
input.setValid(true);
}
else
{
context.addMessage(component.getClientId(context),
new FacesMessage("Invalid e-mail address", null));
input.setValid(false);
}
}

public void testCommand(ActionEvent e)
{
Map project = (Map)testTable.getRowData();
message = (String)project.get("name");
}

public javax.faces.component.UIData getTestTable()
{
return testTable;
}
public void setTestTable(javax.faces.component.UIData testTable)
{
this.testTable = testTable;
}
public double getDoubleValue()
{
return doubleValue;
}
public void setDoubleValue(double doubleValue)
{
this.doubleValue = doubleValue;
}
public javax.faces.model.SelectItem[] getColors()
{
return colors;
}
public void setColors(javax.faces.model.SelectItem[] colors)
{
this.colors = colors;
}
public UserConverter getUserConverter() {
return userConverter;
}
public void setUserConverter(UserConverter userConverter) {
this.userConverter = userConverter;
}

protected void createComponent(Application app, List children,
String componentType, String converterType)
{
/*
HtmlOutputText outputText = application.createComponent(
HtmlOutputText.COMPONENT_TYPE);

UIComponent component = app.createComponent(componentType);
if ((component instanceof UIOutput) && !(component instanceof UISelectBoolean))
{
if (componentType.indexOf("Select") > -1)
{
return;
/* for (int i = 0; i < 50; i++)
{
UISelectItem item = new UISelectItem();
item.setItemLabel(String.valueOf(i));
item.setItemValue(String.valueOf(i));
component.getChildren().add(item);
}*/
}
/* UIOutput label = new UIOutput();
label.setValue(componentType);
children.add(label);


((UIOutput)component).setValue(new Date());
//new User("foo", "foo", RoleType.BUSINESS_ANALYST));
((UIOutput)component).setConverter(app.createConverter(converterType));
children.add(component);
}
}

public void testConverters(ActionEvent e)
{
List children = converterPanel.getChildren();
children.clear();
Application app = FacesContext.getCurrentInstance().getApplication();
Iterator componentTypes = app.getComponentTypes();
while (componentTypes.hasNext())
{
createComponent(app, children, (String)componentTypes.next(), "DateTime");
}
}
*/
public void addMessages(ActionEvent e)
{
FacesContext context = FacesContext.getCurrentInstance();
if (addMessages)
{
context.addMessage(null, new FacesMessage("Your order has been processed successfully.", ""));
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Free shipping limit exceeded.",
"We had to charge your card an extra $25 for shipping."));
}
}
public UserBean getUser()
{
return user;
}
public void setUser(UserBean user)
{
this.user = user;
}
public javax.faces.component.UIViewRoot getView()
{
return view;
}
public void setView(javax.faces.component.UIViewRoot view)
{
this.view = view;
}

public void sendMessage(ActionEvent e)
{
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot view = context.getViewRoot();
HtmlOutputText output =
(HtmlOutputText)view.findComponent("messageForm:outputMessage");
output.setStyle("color:blue");
output.setValue("Who's the Mann?");
}

public void processValueChanged(ValueChangeEvent event)
// throws AbortProcessingException
{
/* UIInput sender = (UIInput)event.getComponent();
sender.setReadonly(true);
changePanel.setRendered(true);*/
}

public javax.faces.component.html.HtmlPanelGrid getChangePanel()
{
return changePanel;
}
public void setChangePanel(javax.faces.component.html.HtmlPanelGrid changePanel)
{
this.changePanel = changePanel;
}
public javax.faces.component.html.HtmlOutputText getOutputText()
{
return outputText;
}
public void setOutputText(javax.faces.component.html.HtmlOutputText outputText)
{
this.outputText = outputText;
}
public javax.faces.component.html.HtmlDataTable getDataTable()
{
if (dataTable == null)
{
String numberStrings[] = new String[] {"one", "two", "three"};
FacesContext facesContext = FacesContext.getCurrentInstance();
dataTable = (HtmlDataTable)facesContext.getApplication().createComponent(
HtmlDataTable.COMPONENT_TYPE);
DataModel myDataModel = new ArrayDataModel(numberStrings);
myDataModel.addDataModelListener(new DataModelListener()
{

public void rowSelected(DataModelEvent e)
{
FacesContext.getCurrentInstance().getExternalContext().
log("phase: " + e.getRowIndex() + "; row seleted:" + e.getRowIndex());
}
});
dataTable.setValue(myDataModel);
}
return dataTable;
}

public void setDataTable(javax.faces.component.html.HtmlDataTable dataTable)
{
this.dataTable = dataTable;
}
public javax.faces.application.Application getApplication()
{
return FacesContext.getCurrentInstance().getApplication();
}
public void setApplication(javax.faces.application.Application application)
{
this.application = application;
}
public boolean isAddMessages()
{
return addMessages;
}
public void setAddMessages(boolean addMessages)
{
this.addMessages = addMessages;
}

public void deleteUser(ActionEvent e)
{
UserBean user = (UserBean)testTable.getRowData();
ArrayList userList = (ArrayList)testTable.getValue();
userList.remove(user);
}

public String testImmediate()
{
FacesContext.getCurrentInstance().getExternalContext().log("Input value = " + message);
return null;
}

}
  回复  更多评论
  

# re: jsf验证的例子 2011-05-02 22:33 代写文章
://www.daixie9.com">www.daixie99.com  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: