在Spring MVC中映射多个控制器

时间:2015-09-08 19:35:39

标签: spring-mvc

按如下方式定义两个控制器用户和数据:

// 1st Controller
@Controller
@RequestMapping(value = {"/", "user"})
public class UserLoginController {
  @Autowired
    private UserLoginService userLoginService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showLoginForm(@ModelAttribute UserLogin userLogin) {
        //model.addAttribute(new UserLogin());
        //System.out.println("showLoginForm() is called");

        return "loginForm";
    }   

}

    //Second COntroller

@Controller
@RequestMapping(value = "user/data/")
public class WorkplanController {

    @Autowired
    private WorkplanService WorkplanService;

    @RequestMapping(value = "importForm", method = RequestMethod.GET)
    public ModelAndView importForm(@ModelAttribute SheetUpload sheetUpload){
        return new ModelAndView("uploadWorkplan");
    }

    @RequestMapping(value= "doUpload", method = RequestMethod.POST)
    public ModelAndView doUpload(@RequestParam CommonsMultipartFile[] uploadFile, SheetUpload fileUpload, Workplan workplan, HttpServletRequest request) {
return new ModelAndView("uploadSucess");
   }

}

当我向doUpload()发出请求时,它显示HTTP Status 400 Error。 我的问题是双重的: 1.为什么我必须包含这样的用户:@RequestMapping(value =“user / data /”)向第二个控制器发出请求为什么不喜欢这个@RequestMapping(value =“data /”)? 2.在第二个控制器中成功调用第二个方法需要更改什么?

以下是我尝试提交的表单:

<form:form action="doUpload"  modelAttribute="sheetUpload" method="post" 
   enctype="multipart/form-data">
   <form:select class="createusers_select_menu" path="From">
      <form:option value="">Select...</form:option>
      <form:option value="A">A</form:option>
      <form:option value="B">B</form:option>
      <form:option value="C">C</form:option>
   </form:select>
   <form:input class="browse_btn" path="uploadFile" type="file" />
   <input type="submit" class="selct_workplan_2_btn" name=""  value=" "/>  
</form:form>

1 个答案:

答案 0 :(得分:0)

  

为什么我必须像这样包含用户:@RequestMapping(value =&#34; user / data /&#34;)向第二个控制器发出请求为什么不喜欢这个@RequestMapping(value =& #34;数据/&#34;?)

你不必。将其更改为@RequestMapping(value="/data")

  

在第二个控制器中成功调用第二个方法需要更改什么?

尝试仅使用单个文件字段,然后报告。网上有很多教程,展示了如何使用Spring上传文件。