This section implements an HtmlInput tag, which is still Noncomposite Component. Accept user input. click the button to display the user input information. Some friends want to see it. I also want to show it, but the Image Upload fails. At the end of this series of articles, I will upload my code.
The basic process is to input data on the initial Page. After clicking the button, the data is generally transmitted to the JSF Serverlet using the PostBack method, and the decode method of the Renderer in the background is passed through FacesContext. getExternalContext (). getRequestParameterMap () obtains the Map object and uses the key to search for user input data. What is the key? ClientIdValue: InputField format is used here.
You need to explain ClientId. JSF defines a rule to know which Tag it is, the programmer can use the following method to output ClientId to the client during page initialization: (in fact, it is generally assigned to the name attribute of the HTML tag)
Writer. writeAttribute ("name", clientIdValue, "clientId"); // The third parameter must be "clientId", which is used internally by JSF.
After the client submits the data, UIComponent can call the member function getClientId (context) to obtain the clientIdValue. In my example, when the page is initialized. getClientId () or clientIdValue and output it to the client to mark my HtmlInput tag, and then get it back when the customer submits it next time. However, my HtmlInput tag contains three sub-tags, one input, one button, and one output. For the first two, ClientId is also required to mark them, I used the parent ClientIdValue + delimiter + sub-tag name. The default Delimiter is :. Therefore, after obtaining the ClientId of the HtmlInput tag, splice the ClientId: "ClientIdValue: InputField" of the input sub-tag and use it as the key to retrieve the user-filled data from the map. Therefore, ClientId is very important for retrieving input data. to retrieve it, you must set ClientId for the required tag during initialization according to the JSF rules, it can be used as a key in Map later.
The delimiters can be modified. You can configure javax. faces. SEPARATOR_CHAR under context-param in the web. xml file to use CSS later.
The Code of Renderer is as follows:
Package com. freebird. renderer;
Import com. freebird. component. HtmlInput;
Import javax. faces. render. Renderer;
Import javax. faces. context. ResponseWriter;
Import java. io. IOException;
Import javax. faces. component. UIComponent;
Import javax. faces. context. FacesContext;
Import javax. faces. component. UINamingContainer;
Import java. util. Map;
/**
* Describe class HtmlInputRenderer here.
*
*
* Created: Wed Dec 29 12:14:24 2010
*
* @ Author <a href = "chenshumailto: chenshu @ csdesktop"> chenshu </a>
* @ Version 1.0
/
Public class HtmlInputRenderer extends Renderer {
Public void decode (FacesContext context, UIComponent component ){
Map requestMap = context. getExternalContext (). getRequestParameterMap ();
String clientId = component. getClientId (context );
Char sep = UINamingContainer. getSeparatorChar (context );
String symbol = (String) requestMap. get (clientId + sep + "inputfield "));
HtmlInput myComponent = (HtmlInput) component;
MyComponent. setSubmittedValue (symbol );
}
@ Override
Public void encodeEnd (FacesContext context, UIComponent) throws IOException {
String clientId = component. getClientId (context );
Char sep = UINamingContainer. getSeparatorChar (context );
EncodeInputField (context, clientId + sep + "inputfield", component );
EncodeSubmitButton (context, clientId + sep + "submit", component );
EncodeOutputField (context, component );
}
Private void encodeInputField (FacesContext context, String clientId, UIComponent component) throws IOException {
// Render a standard HTML input field
ResponseWriter writer = context. getResponseWriter ();
Writer. startElement ("input", component );
Writer. writeAttribute ("type", "text", null );
Writer. writeAttribute ("name", clientId, "clientId ");
HtmlInput myComponent = (HtmlInput) component;
String value = (String) myComponent. getSubmittedValue ();
If (value! = Null ){
Writer. writeAttribute ("value", value, "value ");
}
Writer. writeAttribute ("size", "6", null );
Writer. endElement ("input ");
}
Private void encodeSubmitButton (FacesContext context, String clientId, UIComponent component) throws IOException {
// Render a submit button
ResponseWriter writer = context. getResponseWriter ();
Writer. startElement ("input", component );
Writer. writeAttribute ("type", "Submit", null );
Writer. writeAttribute ("name", clientId, "clientId ");
Writer. writeAttribute ("value", "Click Me! ", Null );
Writer. endElement ("input ");
}
Private void encodeOutputField (FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context. getResponseWriter ();
String hellomsg = (String) component. getAttributes (). get ("value ");
Writer. startElement ("p", component );
Writer. writeText ("You entered:" + hellomsg, null );
Writer. endElement ("p ");
}
}
The Code of UIComponent. The HtmlInput class only inherits the UIInput class, so it naturally has the implementation of the getFamily, get/setSubmittedValue method.
Package com. freebird. component;
Import javax. faces. component. UIInput;
/*
* Describe class HtmlInput here.
*
*
* Created: Wed Dec 29 11:43:18 2010
*
* @ Author <a href = "chenshumailto: chenshu @ csdesktop"> chenshu </a>
* @ Version 1.0
/
Public class HtmlInput extends UIInput {
}
Now let's take a look at two configuration files:
Add in faces-config.xml
<Component>
<Component-type> HtmlInput </component-type>
<Component-class> com. freebird. component. HtmlInput </component-class>
</Component>
<Render-kit>
<Renderer>
<Component-family> javax. faces. Input </component-family>
<Renderer-type> HtmlInputRenderer </renderer-type>
<Renderer-class> com. freebird. renderer. HtmlInputRenderer </renderer-class>
</Renderer>
</Render-kit>
Add in helloworld. taglib. xml
<Tag>
<Tag-name> htmlinput </tag-name>
<Component>
<Component-type> HtmlInput </component-type>
<Renderer-type> HtmlInputRenderer </renderer-type>
</Component>
</Tag>
Using this tag in a web application is simple, just one line:
<Cs: htmlinput/>