2010-12-07 27 views
4

httpリクエストの本体では、パスワードの文字列が渡される方法は "pass = 1111"ですが、beanではパスワードの定義方法は "private String password"です。または私はいつも名前と一致する必要がありますか?Spring-mvcでは、viewの属性名は常にmodelのプロパティ名と一致する必要がありますか?

HTTPリクエストがこの

curl -H "Accept:text/html" -H "Content-Type application/x-www-form-urlencoded" -d 'email=test%40gmail.com&pass=1111&passconfirm=1111&name=x+y' "http://localhost:8080/project/register" 

ハンドラメソッドのようなものです

@RequestMapping(method = RequestMethod.POST, headers = "content-type=application/x-www-form-urlencoded") 
public String register(@ModelAttribute UserAccountBean account) ... 

UserAccountBeanが

です
public class UserAccountBean2 { 
    @NotNull 
    @Size(min = 1, max = 25) 
    private String name; 

    @NotNull 
    @Size(min = 4, max = 8) 
    private String password; 

    @NotNull 
    private String email; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getPassword() 
    { 
     return password; 
    } 

    public void setPassword(String password) 
    { 
     this.password = password; 
    } 

    public String toString() { 
     return new ToStringCreator(this).append("name", name).append("password", password).toString(); 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
} 
+0

この問題の解決策については、http://stackoverflow.com/q/8986593を参照してください。 – Simon

答えて

3

で@InitBinderアノテーション付きメソッド@RequestParam注釈を使用すると、手動で任意の値を設定します。 UserControllerで

@InitBinder(value="user") 
    public void bind(WebDataBinder dataBinder, WebRequest webRequest, @RequestParam(value="pass", required=false) String password) { 
     User user = (User) dataBinder.getTarget(); 
     user.setPassword(password); 
    } 

が、私は にアノテーションを使用することができる方法が違いを扱うか、私は常に名前と一致 に持っていますか?

AFAIK問題を解決できる既成の注釈がSpring MVCにはありません。状況を処理するためにカスタムセットアップが必要です。

WebModelAttribute

@Target({ElementType.METHOD, ElementType.PARAMETER}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface WebModelAttribute { 
String modelAttributeName(); 
    WebParameterMapping[] parameterMappings(); 
} 

WebParameterMapping

@Target({ElementType.METHOD, ElementType.PARAMETER}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface WebParameterMapping { 
    String webProperty(); 
    String beanProperty(); 
} 

UserControllerで

@Controller 
public class UserController extends AbstractController { 

    @Override 
    @InitBinder(value="user") 
    @WebModelAttribute(modelAttributeName="user", parameterMappings={@WebParameterMapping(webProperty="pass", beanProperty="password")}) 
    protected void bindWebParameters(WebDataBinder dataBinder, WebRequest webRequest, WebParameterResolver mappingResolver) { 
     super.bindWebParameters(dataBinder, webRequest, mappingResolver); 
    } 

AbstractController

public class AbstractController { 

    protected void bindWebParameters(WebDataBinder dataBinder, WebRequest webRequest, WebParameterResolver mappingResolver) { 
     if(mappingResolver != null && dataBinder.getTarget() != null && dataBinder.getObjectName().equals(mappingResolver.getModelAttributeName())) { 
      String[] allowedFields = mappingResolver.getAllowedFields(dataBinder.getAllowedFields()); 
      String[] disallowedFields = mappingResolver.getDisallowedFields(dataBinder.getDisallowedFields()); 

      dataBinder.setAllowedFields(allowedFields); 
      dataBinder.setDisallowedFields(disallowedFields); 

      dataBinder.bind(mappingResolver.getPropertyValues(dataBinder, webRequest)); 
     } 
    } 

} 

WebParameterResolver

public class WebParameterResolver { 

    private String modelAttributeName; 
    private WebParameterMapping[] parameterMappings; 

    public WebParameterResolver(String modelAttributeName, 
      WebParameterMapping[] parameterMappings) { 
     this.modelAttributeName = modelAttributeName; 
     this.parameterMappings = parameterMappings; 
    } 

    public String getModelAttributeName() { 
     return modelAttributeName; 
    } 

    public String[] getDisallowedFields(String[] existingDisallowedFields) { 
     List<String> disallowedFields = new ArrayList<String>(); 
     for (WebParameterMapping parameterMapping : parameterMappings) { 
      disallowedFields.add(parameterMapping.webProperty()); 
     } 
     if (existingDisallowedFields != null) { 
      for (String disallowedField : existingDisallowedFields) { 
       disallowedFields.add(disallowedField); 
      } 
     } 
     return disallowedFields.toArray(new String[disallowedFields.size()]); 
    } 

    public String[] getAllowedFields(String[] existingAllowedFields) { 
     List<String> allowedFields = new ArrayList<String>(); 
     for (WebParameterMapping parameterMapping : parameterMappings) { 
      allowedFields.add(parameterMapping.beanProperty()); 
     } 
     if (existingAllowedFields != null) { 
      for (String allowedField : existingAllowedFields) { 
       allowedFields.add(allowedField); 
      } 
     } 
     return allowedFields.toArray(new String[allowedFields.size()]); 
    } 

    public MutablePropertyValues getPropertyValues(WebDataBinder dataBinder, 
      WebRequest webRequest) { 
     MutablePropertyValues propertyValues = new MutablePropertyValues(); 
     for (WebParameterMapping parameterMapping : parameterMappings) { 
      String[] values = webRequest.getParameterValues(parameterMapping.webProperty()); 
      if (values == null || values.length == 0) { 
       // do nothing 
      } else if (values.length == 1) { 
       propertyValues.add(parameterMapping.beanProperty(), values[0]); 
      } else { 
       propertyValues.add(parameterMapping.beanProperty(), values); 
      } 
     } 
     dataBinder.bind(propertyValues); 
     return propertyValues; 
    } 

} 

CustomArgumentResolver

public class CustomArgumentResolver implements WebArgumentResolver { 

    @Override 
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception { 

     if(methodParameter.getParameterType().equals(WebParameterResolver.class)) { 
      WebModelAttribute webModelAttribute = methodParameter.getMethod().getAnnotation(WebModelAttribute.class); 
      if(webModelAttribute == null) { 
       throw new RuntimeException("method must have WebModelAttribute"); 
      } 
      return new WebParameterResolver(webModelAttribute.modelAttributeName(), webModelAttribute.parameterMappings()); 
     } 

     return UNRESOLVED; 
    } 

} 

豆。XML

<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="customArgumentResolvers" ref="timetracking.annotations.CustomArgumentResolver"/> 
    </bean> 
<bean name="timetracking.annotations.CustomArgumentResolver" 
     class="timetracking.annotations.CustomArgumentResolver" /> 

また、いくつかのヘルパークラスでpublic static void bindWebParameters(...)メソッドを持つことができます。毎回AbstractControllerを拡張する必要はありません。

0

3.0にアノテーションベースのソリューションはありません。

追加のgetPass()setPass(String pass)メソッドを追加するだけで設定する必要があります。

4

あなたはこれでそれを達成することができます

@RequestMapping(method = RequestMethod.POST, headers = "content-type=application/x-www-form-urlencoded") 
public String register(@ModelAttribute("userAccountBean") UserAccountBean account) ... 

@ModelAttribute("userAccountBean") 
public UserAccountBean getUserAccountBean(HttpServletRequest req) { 
    UserAccountBean uab = new UserAccountBean(); 
    uab.setPassword(req.getParameter("pass")); 
    return uab; 
} 
関連する問題