Spring MVC hidden value example

来源:互联网 发布:cpu淘宝e5水多深 编辑:程序博客网 时间:2024/05/16 08:46

In Spring MVC, you can use <form:hidden /> to render a HTML hidden value field. For example,

    <form:hidden path="secretValue" />

It will render the following HTML code

    <input id="secretValue" name="secretValue" type="hidden" value="I'm hidden value"/> 

P.S Assume “secretValue” property contains value “I’m hidden value”.

In this tutorial, we show you how to use Spring’s form tag “<form:hidden />” to render a HTML hidden value.

1. Controller

A SimpleFormController to handle the form hidden value, and initialize the hidden value with “I’m hidden value, hehe”.

File : HiddenController.java

package com.mkyong.customer.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;import com.mkyong.customer.model.Customer;public class HiddenController extends SimpleFormController{    public HiddenController(){        setCommandClass(Customer.class);        setCommandName("customerForm");    }    @Override    protected Object formBackingObject(HttpServletRequest request)        throws Exception {        Customer cust = new Customer();        cust.setSecretValue("I'm hidden value, hehe");        return cust;    }    @Override    protected ModelAndView onSubmit(HttpServletRequest request,        HttpServletResponse response, Object command, BindException errors)        throws Exception {        Customer customer = (Customer)command;        return new ModelAndView("CustomerSuccess","customer",customer);    }}

2. Model

A Customer object to store the form hidden value.

File : Customer.java

package com.mkyong.customer.model;public class Customer{    String secretValue;    //getter and setter methods}

3. View

A JSP page to use the Spring’s form tag “<form:hidden />” to render a HTML hidden value.

File : CustomerForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%><html><body>    <h2>Spring's form hidden example</h2>    <form:form method="POST" commandName="customerForm">        <table>            <tr>                <td>Hidden value (view source to see it) :</td>                <td><form:hidden path="secretValue" /></td>            </tr>            <tr>                <td><input type="submit" /></td>            </tr>        </table>    </form:form></body></html>

If the form is submitted, render the successful page and display the submitted hidden value.

File : CustomerSuccess.jsp

<html><body>    <h2>Spring's form hidden value example</h2>    Hidden value : ${customer.secretValue}    <br /></body></html>

4. Spring Bean Configuration

Link it all ~

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  <bean  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />    <bean class="com.mkyong.customer.controller.HiddenController">        <property name="formView" value="CustomerForm" />        <property name="successView" value="CustomerSuccess" />    </bean>    <bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <value>/WEB-INF/pages/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean></beans>

5. Demo

Access the page – http://localhost:8080/SpringMVCForm/hidden.htm

SpringMVC-Hidden-Example-1
If the form is submitted successfully, just display the submitted hidden value.

SpringMVC-Hidden-Example-2

0 0