簡體   English   中英

如何以spring形式綁定子類對象提交為modelAttribute

[英]How to bind subclass object in spring form submit as modelAttribute

我有

Class Shape {
      //Implementation
}
Class Round extends Shape{
      //Implementation
}

控制器我有

@Requestmapping(value="/view/form")
public ModelAndView getForm(){
ModelAndView mav=new ModelAndView();
mav.addObject("shape",new Round());
}


@RequestMapping(value="/submit", method = RequestMethod.POST)    
public ModelAndView submitForm(@ModelAttribute("shape") Shape shape){
         if(shape instanceof Round){ //**Not giving positive result**

         }
    }

在Jsp

<form:form action="/submit" commandName="shape" method="post">

//form tags

</form:form>

當我用Round對象提交表單時。 在控制器端,ModelAttribute沒有給出Round的實例。 它只給出形狀的例子。 這該怎么做

這永遠不會奏效

<form:form action="/submit" commandName="shape" method="post">

您正在從表單中提交shape並期望控制器方法中的shape

public ModelAndView submitForm(@ModelAttribute("shape") Shape shape)

永遠不會給你一個Round對象。

只需從表單中提交一個Round對象並使用它。

 <form:form action="/submit" commandName="round" method="post">
 public ModelAndView submitForm(@ModelAttribute("round") Round round)

編輯: -

在表單中有一個hiddenInput類型,告訴controller它傳遞的Shape類型,你可以根據用戶請求動態更改隱藏標簽的值。

<input type="hidden" name="type" value="round">

進去類型的值contoller ,並用它來castShape對象

     public ModelAndView submitForm(
     @ModelAttribute("shape") Shape shape,
     @RequestParam("type") String type)
     {
     if(type.equals("round")){
      //if type is a round you can cast the shape to a round
      Round round = (Round)shape; 
       }
    }

你不能做這個。 因為這是兩個不同的請求生命周期。

@Requestmapping(value="/view/form")
public ModelAndView getForm(){
ModelAndView mav=new ModelAndView();
mav.addObject("shape",new Round());
}

當執行上述請求時,即使您在mav添加了Round對象,它也已轉換為html響應並發送回客戶端。

因此,當您提交表單時接下來請求時,它是一個完全獨立的請求,並且spring無法識別為先前的請求添加了哪個對象類型。

@RequestMapping(value="/submit", method = RequestMethod.POST)    
public ModelAndView submitForm(@ModelAttribute("shape") Shape shape){
         if(shape instanceof Round){ //**Not giving positive result**

         }
    }

但是你可以嘗試探索@SessionAttributes ,使用它可以在不同的請求中維護相同的對象

可以指定需要綁定的子類。 您必須在表單中添加一個附加參數(隱藏輸入),指定需要綁定的類型。 此字段必須與此案例形狀中的模型屬性具有相同的名稱。 然后,您需要實現一個轉換器,將此字符串參數值轉換為您需要綁定到的實際實例

以下是您需要實施的更改

1)在你的jsp中添加隱藏的輸入

<form:form action="/submit" commandName="shape" method="post">
   <input type="hidden" name="shape" value="round"/>
//other form tags
</form:form>

2)實現轉換器以從String轉換為Shape

public class StringToShapeConverter implements Converter<String,Shape>{

   public Shape convert(String source){
      if("round".equalsIgnoreCase(source)){
          return new Round();
      }
      //other shapes
   }
}

3)然后注冊您的轉換器,以便Spring MVC知道它。 如果您使用的是Java配置,則需要擴展WebMvcConfigurerAdapter並覆蓋addFormatters方法

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{

   @Override
   public void addFormatters(FormatterRegistry registry){
      registry.addConverter(new StringToShapeConverter());
   }
}

如果您使用的是xml配置,則可以使用mvc:annotation-driven元素指定要使用的轉換服務。 然后使用FormattingConversionSErviceFactoryBean注冊轉換器

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven conversion-service="conversionService"/>

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
       <property name="converters">
           <set>
              <bean class="some.package.StringToShapeConverter"/>
           </set>
       </property>
    </bean>
</beans>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM