2015-10-20 49 views
0

我使用Thymeleaf和HTML作爲視圖部分。和春天3.有一個單選按鈕的列表,你應該只選擇一個。我驗證使用在url中將url連接到控制器類mvc

   `<script th:inline="javascript"> 
        /*<![CDATA[*/ 
          function choose_report_kind (form_name) { 
           var form = document.forms[form_name]; 
           var group = form.elements['report_kind']; 

           var report_kind; 
           for (var i = 0; i < group.length; i++) { 
            if (group[i].checked == true) { 
             report_kind = group[i].value; 
            } 
           } 

           var url; 
           if (report_kind == "aos") { 
            url = "/CustomReports?section=choose_organization"; 
           } 
           else if (report_kind == 'aos_2007') { 
            url = "/MarriottReporting1/CustomReports/2007?section=choose_organization"; 
           } 
           else if (report_kind == 'aos_2008') { 
            url = "2008/CustomReports?section=choose_organization"; 
           } 
           else if (report_kind == '2009_pulse') { 
            url = "pulse/2009/CustomReports?section=choose_organization"; 
           } 
           else { 
            alert("You must choose a type of report to run"); 
            return; 
           } 
           window.document.location.href = url; 
          } 
          /*]]>*/ 
          </script>` 

我必須通過此URL到我的控制器。

@RequestMapping(value="/CustomReports/{year}?section=choose_organization", method=RequestMethod.GET) 
public ModelAndView customReportsHome(@RequestParam("section") String section,@PathVariable("year") String year,HttpSession session, Map<String, Object> map){ 
    logger.debug("INSIDE CUSTOMREPORTHOME REQUEST PARAM"); 
    return new ModelAndView("CustomReports"); 
} 

但我沒能得到任何東西,我得到錯誤說CustomReport not found.

+0

你的春天RequesMapping不列入匹配在JavaScript中的url模式。 – Sachin

+0

你能幫我解釋一下它應該如何,因爲我對此很陌生? – nitheesh

+0

看看你在那裏的4個url模式..我認爲這個@RequestMapping對我更有意義.. @RequestMapping(「/ CustomReports/{section})'然後使用'@RequestParam(」year「)'和'@PathVariable(「section」)'來獲取它們 – Sachin

回答

0

我覺得RequesMapping亙古不匹配的JavaScript URL模式。

望着那你有4種URL模式.. 嘗試這些變化...

的Javascript:

if (report_kind == "aos") { 
    url = "/CustomReports/choose_organization"; 
} else if (report_kind == 'aos_2007') { 
    url = "/MarriottReporting1/CustomReports/choose_organization?year=2007"; 
} else if (report_kind == 'aos_2008') { 
    url = "2008/CustomReports/choose_organization?year=2008"; 
} else if (report_kind == '2009_pulse') { 
    url = "pulse/2009/CustomReports/choose_organization?year=2009"; 
} 

控制器:

@RequestMapping("/CustomReports/{section}") 
public ModelAndView customReportsHome(@PathVariable("section") String section,@RequestParam(value="year", required = false) String year,HttpSession session, Map<String, Object> map){ 
    logger.debug("INSIDE CUSTOMREPORTHOME REQUEST PARAM"); 
    return new ModelAndView("CustomReports"); 
} 
+0

我不確定Map map'會在控制器中給你什麼。如果spring不能綁定,那麼你的控制器方法不能被 – Sachin

+0

我仍然遇到同樣的錯誤,你能告訴我如何從「window.document.location.href = url;」調用控制器嗎? – nitheesh

+0

你需要爲你的url添加上下文路徑。 ,url需要加前綴,如'http:// /'..所以完全限定的賦值就像這樣'window.document.location.href =「http:// // MarriottRep orting1/CustomReports/choose_organization?year = 2007「;'檢查http://www.w3schools.com/js/js_window_location.asp或https://developer.mozilla.org/en-US/docs/Web/API/Window /位置來了解該分配的功能。 – Sachin