2011-06-22 33 views
1

私は国際化対応のWebプロジェクトを持っています。それはhttp://www.springbyexample.org/examples/basic-webapp-internationalization-jsp-example.htmlとかなり似ています。 デコレータにロケールを切り替えるためのリンクがあります。現在のURLにlang paramを追加します:Spring国際化:ロケール変更の問題

<a href="?lang=en">En</a> | <a href="?lang=ru">Ru</a> </span></td> 

初めて国際化がうまくいきました。しかし、我々はいくつかのフォームで問題を発見した。 フォーム:

<form:form action="${pageContext.request.contextPath}/branch/${branchId}/topic.html" modelAttribute="topicDto" method="POST" 
onsubmit="this.getAttribute('submitted')"> <!--Block multiple form submissions--> 
<table border="2" width="100%"> 
    <tr> 
     <td width="30%"> 
      <form:label path="topicName"><spring:message code="label.topic"/></form:label> 
      <form:input path="topicName"/> 
      <form:errors path="topicName"/> 
     </td> 
    </tr> 
    <tr> 
     <td height="200"> 
      <form:label path="bodyText"><spring:message code="label.text"/></form:label> 
      <form:textarea path="bodyText"/> 
      <form:errors path="bodyText"/> 
     </td> 
    </tr> 
</table> 
<input type="submit" value="<spring:message code="label.addtopic"/>"/> 

コントローラ:

/** 
* @see Topic 
*/ 
@Controller 
public final class TopicController { 
/** 
* Method handles newTopic.html GET request and display page for creation new topic 
* 
* @param branchId {@link org.jtalks.jcommune.model.entity.Branch} id 
* @return {@code ModelAndView} object with "newTopic" view, new {@link TopicDto} and branch id 
*/ 
@RequestMapping(value = "/branch/{branchId}/topic/create", method = RequestMethod.GET) 
public ModelAndView createPage(@PathVariable("branchId") Long branchId) { 
    return new ModelAndView("newTopic") 
      .addObject("topicDto", new TopicDto()) 
      .addObject("branchId", branchId); 
} 

/** 
* This method handles POST requests, it will be always activated when the user pressing "Submit topic" 
* 
* @param topicDto the object that provides communication between spring form and controller 
* @param result {@link BindingResult} object for spring validation 
* @param branchId hold the current branchId 
* @return {@code ModelAndView} object which will be redirect to forum.html 
* @throws org.jtalks.jcommune.service.exceptions.NotFoundException 
*   when branch not found 
*/ 
@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST) 
public ModelAndView create(@Valid @ModelAttribute TopicDto topicDto, 
          BindingResult result, 
          @PathVariable("branchId") Long branchId) throws NotFoundException { 
    if (result.hasErrors()) { 
     return new ModelAndView("newTopic").addObject("branchId", branchId); 
    } else { 
     Topic createdTopic = topicService.createTopic(topicDto.getTopicName(), topicDto.getBodyText(), 
       branchId); 
     return new ModelAndView("redirect:/branch/" + branchId + "/topic/" 
       + createdTopic.getId() + ".html"); 
    } 
} 

}

ユーザーはそれがフィールドの前に検証メッセージが表示されます、無効なフィールドを持つフォームを投稿した場合。あなたが登録ページ http://deploy.jtalks.org/jcommune/registration.html で当社の開発サーバー例えば http://deploy.jtalks.org/jcommune/index.html で問題を自分でチェックすることができ

HTTP Status 405 - Request method 'GET' not supported 
type Status report 
message Request method 'GET' not supported 
description The specified HTTP method is not allowed for the requested resource (Request method  'GET' not supported). 
Apache Tomcat/7.0.11 

空白のフォームのままにし、それを提出する:彼はその瞬間にページの言語を切り替えると、彼はエラーが表示されます。検証メッセージが表示されます。言語を変更し、フォームをもう一度送信して、指定されたエラーを表示するよりも。

あなたがページにもたらしたものは何でもURLにLANGパラメータを追加しているここに私たちのすべてのソースhttp://fisheye.jtalks.org/

答えて

0

Iは

@RequestMapping(value = "/branch/{branchId}/topic", method = {RequestMethod.POST, RequestMethod.GET}) 

@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST) 

からのマッピングを変更することによってporblemを固定し、エラーがなくなっています。

1

を見つけることができます。したがって、/registration.htmlを介してフォームにアクセスすると、EnとRuのリンクも/registration.htmlにつながります。コントローラがGETをサポートしていると思われるので、リンクをクリックしてGETリクエストを生成すると、すべてが機能します。

フォームをポストしたら、EnとRuのリンクは/user.htmlにつながります。なぜなら、ページをどのようにプログラミングしたかということです。これらのリンクをクリックすると、/ user.htmlへのGETリクエストが生成されます。 /user.htmlのコントローラはPOSTをサポートしていて、GETはサポートしていないため、おそらくこれは失敗します。

FYIでは、クライアント側とサーバー側の両方で何が起こっているのかを正確に確認できます。どちらの場合でも、いくつものツールを使用して、HTTPトラフィックの往復を監視します。

切り替え言語がサーバー側で行われるような方法でSpringを使用している可能性があるので、実際のページを再度取得する必要があります。特に登​​録フォームを修正するには、ユーザーが既に入力したデータを投げて、フォーム検証が失敗した後でも/registration.htmlを参照するようにリンクを切り替えるのが最も簡単です。 (言い換えれば、リンクはあなたをページに導いたURLに基​​づいて生成すべきではありません)。ユーザーは途中で言語を切り替えることはできません。ただし、フォームデータを保存する必要がある場合は、別の方法が必要です。

+0

ありがとうございます。しかし、私はこのエラーを避けるために何ができますか?ユーザーをメインページにリダイレクトすることができます。しかし、ユーザーフレンドリーではありません。コントローラの前のどこかでGETパラメータを確認できますか? – NullPointer

+0

GET-paramを渡すのではなく、JavaScript固有のクッキーフォームをjavascriptに設定することもできます。それはうまくいくが、私はそれが最善の解決策ではないと思う。 – NullPointer

0

POST/REDIRECT/GET後のパターンを使用する必要があります。例えば

http://en.wikipedia.org/wiki/Post/Redirect/Get

:あなたは、リダイレクト後に呼び出される(view)メソッドにデータ(オブジェクト)を渡す必要があります場合は

@Controller 
public final class TopicController { 

@RequestMapping(value = "/branch/{branchId}/topic/create", method = RequestMethod.GET) 
public ModelAndView createPage(@PathVariable("branchId") Long branchId) { 
    return new ModelAndView("newTopic") 
      .addObject("topicDto", new TopicDto()) 
      .addObject("branchId", branchId); 
} 

@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST) 
public ModelAndView create(@Valid @ModelAttribute TopicDto topicDto, 
          BindingResult result, 
          @PathVariable("branchId") Long branchId) throws NotFoundException { 
    if (result.hasErrors()) { 
     return new ModelAndView("newTopic").addObject("branchId", branchId); 
    } else { 

     /*Here you must create your topic, and add to DB, then redirect..*/ 

     return new ModelAndView("redirect:/TO_THE_VIEW_PAGE"); 
    } 
} 

@RequestMapping(value = "/TO_THE_VIEW_PAGE", method = RequestMethod.GET) 
public ModelAndView view(/*need parameters*/) { 

    /*...There you must get your topic from DB, and go to view page..*/ 

    return new ModelAndView("/PATH_TO_THE_VIEW_PAGE"); 
} 

、あなたはRedirectAttributesクラスを使用することができます。例えば http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

:次に

@RequestMapping(value = "/branch/{branchId}/topic", method = RequestMethod.POST) 
public ModelAndView create(@Valid @ModelAttribute TopicDto topicDto, 
          BindingResult result, 
          @PathVariable("branchId") Long branchId, 
          RedirectAttributes redirectAttributes) throws NotFoundException { 

     redirectAttributes.addFlashAttribute("topic", topicDto); 
     redirectAttributes.addFlashAttribute("branchId", branchId); 

     return new ModelAndView("redirect:/TO_THE_VIEW_PAGE"); 
    } 
} 

view方法でそれらの属性を取得します。