Spring MVCの隠された値の例
Spring MVCでは、<form:hidden />を使用してHTMLの非表示値フィールドをレンダリングできます。 例えば、
次のHTMLコードをレンダリングします
P.S Assume “secretValue” property contains value “I’m hidden value”.
このチュートリアルでは、Springのフォームタグ「<form:hidden />」をrender a HTML hidden valueに使用する方法を示します。
1. コントローラ
フォームの非表示値を処理し、非表示値を「I’m hidden value、hehe」で初期化するためのSimpleFormController。
ファイル:HiddenController.java
package com.example.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.example.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オブジェクト。
ファイル:Customer.java
package com.example.customer.model;
public class Customer{
String secretValue;
//getter and setter methods
}
3. View
Springのフォームタグ「<form:hidden />」を使用してHTMLの非表示値をレンダリングするJSPページ。
ファイル:CustomerForm.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
Spring's form hidden example
Hidden value (view source to see it) :
フォームが送信された場合、成功したページをレンダリングし、送信された非表示の値を表示します。
ファイル:CustomerSuccess.jsp
Spring's form hidden value example
Hidden value : ${customer.secretValue}
4. Spring Beanの構成
それをすべてリンクしてください〜
/WEB-INF/pages/ .jsp
5. Demo
ページへのアクセス - http://localhost:8080/SpringMVCForm/hidden.htm

フォームが正常に送信された場合、送信された非表示の値を表示するだけです。

ソースコードをダウンロード
ダウンロード–SpringMVCForm-HiddenValue-Example.zip(8KB)