Spring MVCでは、
<form:hidden/>
を使用してHTMLの隠し値フィールドをレンダリングできます。例えば、
<form:hidden path="secretValue"/>
次のHTMLコードがレンダリングされます
<input id="secretValue" name="secretValue" type="hidden" value="I'm hidden value"/>
__P.S ”
secretValue
“プロパティに値 “I’m hidden value”が含まれていると仮定します。
このチュートリアルでは、Springのフォームタグ ”
<form:hidden/>
“を使用してHTMLの隠し値** をレンダリングする方法を説明します。
1.コントローラー
フォームの隠し値を処理し、隠し値を “私は隠し値、hehe”で初期化するための `SimpleFormController`です。
ファイル:隠しController.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.モデル
フォームの隠し値を格納するCustomerオブジェクト。
File:Customer.java
package com.mkyong.customer.model;
public class Customer{
String secretValue;
//getter and setter methods
}
3.表示
Springのフォームタグ ”
<form:hidden/>
“を使用してHTMLの隠し値をレンダリングするJSPページ。
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>
フォームが送信された場合は、成功したページをレンダリングし、送信された隠し値を表示します。
ファイル:Customer Success.jsp
<html>
<body>
<h2>Spring's form hidden value example</h2>
Hidden value : ${customer.secretValue}
<br/>
</body>
</html>
4. Spring Beanの設定
すべてのリンク〜
<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.デモ
ページにアクセスする –
http://localhost:8080/SpringMVCForm/hidden.htm
フォームが正常に送信された場合は、送信された隠し値を表示するだけです。