2017-01-20 7 views
0

リダイレクトが正しく機能しません。私は春に非常に新しいので、私は問題を理解することができませんでした。spring mvcを使用してリダイレクトが正しく機能しない

私のフォームを送信するとき、私のコントローラは( "schoolform")submitFormコントローラが呼び出され、別のコントローラに( 'form')フォームコントローラにリダイレクトされますが、ログインコントローラに移動します。どうしてか分かりません ? schoolformをフォームコントローラにリダイレクトしたいと思います。

@RequestMapping(value = "/schoolform", method = RequestMethod.POST) 
    public String submitForm(@ModelAttribute("school")School school,Model model,HttpServletRequest request,HttpServletResponse resp) { 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     schoolService.update(school); 
      System.out.println("Form submitted finaly, No further changes can be made."); 
     return "redirect:/form.html"; 
    } 


@RequestMapping(value = "/form", method = RequestMethod.GET) 
    public String form(Model model,HttpServletRequest request) { 
     HttpSession session = request.getSession(true); 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
     String name = auth.getName(); // get logged in username 
     System.out.println(name+"--------form page-----"); 

    } 

@RequestMapping(value = "/login", method = RequestMethod.GET) 
    public ModelAndView login(
      @RequestParam(value = "error", required = false) String error, 
      @RequestParam(value = "logout", required = false) String logout) { 
     logger.info("------------------LoginController ---------------"); 
     System.out.println("LoginController "); 
     ModelAndView model = new ModelAndView(); 
     if (error != null) { 
      model.addObject("error", "Invalid username and password!"); 
     } 

     if (logout != null) { 
      model.addObject("msg", "You've been logged out successfully."); 

     } 
     model.setViewName("login"); 
     return model; 

    } 
+0

:今、/form.htmlを受け入れ、あなたのコントローラにはこの方法は、だから今、あなたがgottoそのマッピング値/form.htmlであり、それはGET要求を受け入れ、あなたのコントローラクラスのメソッドを持って、ありません。/form'、 '.html'拡張子なし –

+0

それはうまくいきません。 –

+0

何が起こっているのか疑問からは明らかではありません。あなたはフォーム提出で打っているURLをどのURLにリダイレクトしていますか?あなたが達成したいと思う行動は何ですか? –

答えて

0

私はあなたのURLにリダイレクトしようとしている方法は、POST要求を受け入れるので、それが機能していないと思います。 POSTの方法からリダイレクトすることはできませんUNLESSGETメソッドを受け入れ、リダイレクトしようとしている値を受け入れるハンドラメソッドが@RequestMappingです。

したがって、基本的には、POST要求のみを受け入れる方法submitFormは、/form.htmlにリダイレクトしようとしています。 `リダイレクトを返すようにしてください

@RequestMapping(value = "/form.html", method = RequestMethod.GET) 
public String methodName(arg1 ..){ ... } 
関連する問題