..


Sponsored Links

Web - View Resolver and Spring Forms TLDs

Spring View has many Resolver, which are designed to render the model in the browser. We will analyze only the 'who is that InternalResourceViewResolver Suppot Servlet and JSP.

As seen previously through each controller returns the logical name of a view that is resolved through the view resolvers, in our case the final view is a JSP.
InternalResourceViewResolver enough to configure the following lines in [dispatcherservlet]-servlet.xml:






 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">



  



 <property name="prefix" value="/WEB-INF/jsp/"/>



  



 <property name="suffix" value=".jsp"/>







 </ Bean>



Through this configuration, when the controller return a string like / path / Resolver View our result will be a forward to jsp / WEB-INF/jsp/path/result.jsp. Invce If you need to do a redirect, simply prefix the string back controller "redirect".

Once configured Our View Resolver, we can only render the data that have been made by the controller in the Model.
Using the jsp you can use JSTL to render the data. For the construction of form, contains the Spring-form.tld spring, we see the tags:
  • form - main tag, all tags of the library must be contained in this tag to work properly. This tag also takes care of adding to it a model of the objects in the pageContext the JSP tags so that children have access to them.
  • input - that renders an input tag with type 'text'.
  • checkbox - tag renders a checkbox. This tag refers all'attibuto a bean that can be a collection, a boolean or an object.
  • checkboxes - tag renders a set of checkboxes. This tag refers all'attibuto a bean which is a list of possible values.
  • radiobutton - tag renders a radio button.
  • radiobuttons - tag renders a set of radiobuttons that refer to a single attribute of a bean.
  • password - that renders an input tag with type 'password'.
  • select - renders a select tag.
  • option - the tag that renders an option.
  • options - the tag that renders a list of options.
  • textarea - renders a textarea tag.
  • hidden - that renders an input tag with type 'hidden'.
  • errors - renders a span tag that contains the validation errors of form.

We see a complete example of all the tags:






 @ Controller







 {public class FormController





  



 RequestMapping @ ("formExample")



  



 public String viewForm (Model model) {



    



 / / Add the beans in my model



    



 FormBean formBean FormBean = new ();



    



 formBean.setHidden ("hiddenValue");





    



 model.addAttribute ("formBean" formBean);





    



 Collection <String> <String> checkboxes = new ArrayList ();



    



 checkBoxes.add ("1");



    



 checkBoxes.add ("2");



    



 checkBoxes.add ("3");



    



 model.addAttribute ("CheckBoxes" checkboxes);





    



 Collection <String> <String> radiobuttons = new ArrayList ();



    



 radiobuttons.add ("1");



    



 radiobuttons.add ("2");



    



 radiobuttons.add ("3");



    



 model.addAttribute ("radiobuttons" checkboxes);





    



 Collection options = new ArrayList <OptionForm> <OptionForm> ();





    



 OptionForm option1 = new OptionForm ();



    



 option1.setLabel ("option1");



    



 option1.setValue ("1");



    



 options.add (option1);





    



 OptionForm OptionForm option2 = new ();



    



 option2.setLabel ("option2");



    



 option2.setValue ("2");



    



 options.add (option2);



    



 model.addAttribute ("options", options);





    



 / / Call the view resolver



    



 return "form / example";



  



 }





  



 @ RequestMapping (value = "submit" method = RequestMethod.POST)



  



 public String viewForm (FormBean b) {



    



 / / Call the view resolver



    



 return "form / ok";



  



 }







 }



our form:





 <form:form commandName="formBean" action="submit">







 <p>



  



 <form:label path="input"> input </ form: label>



  



 <form:input path="input"/>







 </ P>







 <p>



  



 <form:label path="check1"> check1 </ form: label>



  



 <form:checkbox path="check1"/>







 </ P>







 <p>



  



 <form:label path="check2"> check2 </ form: label>



  



 <form:checkbox path="check2" value="value1" label="value1"/>



  



 <form:checkbox path="check2" value="value2" label="value2"/>



  



 <form:checkbox path="check2" value="value3" label="value3"/>







 </ P>







 <p>



  



 <form:label path="check3"> check3 </ form: label>



  



 <form:checkbox path="check3" value="check3"/>







 </ P>







 <p>



  



 <form:label path="checkboxes"> checkboxes </ form: label>



  



 <form:checkboxes path="checkboxes" items="${checkBoxes}"/>







 </ P>







 <p>



  



 <form:label path="radiobutton"> radiobutton </ form: label>



  



 <form:radiobutton path="radiobutton" value="radiobutton1" label="radiobutton1"/>



  



 <form:radiobutton path="radiobutton" value="radiobutton2" label="radiobutton2"/>







 </ P>







 <p>



  



 <form:label path="radiobuttons"> radiobuttons </ form: label>



  



 <form:radiobuttons path="radiobuttons" items="${radiobuttons}"/>







 </ P>







 <p>



  



 <form:label path="password"> password </ form: label>



  



 <form:password path="password"/>







 </ P>







 <p>



  



 <form:label path="select"> select </ form: label>



  



 <form:select path="select">



    



 <form:option value="-1" label="-- select" />



    



 <form:options items="${options}" itemLabel="label" itemValue="value" />



  



 </ Form: select>







 </ P>







 <p>



  



 <form:label path="textarea"> textarea </ form: label>



  



 <form:textarea path="textarea"/>







 </ P>







 <p>



  



 <form:label path="hidden"> hidden </ form: label>



  



 <form:hidden path="hidden"/>







 </ P>









 <input type="submit" value="submit" />







 </ Form: form>



Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links