如何访问模型属性的值并设置为 ModelAndView

How to access Model Attribute's value and set to ModelAndView

我似乎找不到解决方案。

我有 returns ModelAndView 查看网页的方法。

只要条件为真,我就会存储一个网页文件名来建模。

示例:

    @RequestMapping(value = "/process", method = RequestMethod.POST)
    public ModelAndView processRequest(HttpServletRequest request,
    HttpServletRequest response, Model model,@RequestParam("file") MultipartFile file) 
    throws IOException {
        
        if (file.isEmpty()) {
             model.addAttribute("exception", "web_file1")
        } else {
             model.addAttribute("exception", "web_file2")
        }
          
            

如何获取“异常”中存储的数据并将其设置为ModelAndView?

                ModelAndView mav = new ModelAndView();
                mav.setViewName("exception");  
                //expected:web_file2 
                //actual:exception
                return mav;
        
1.  put your  data using 
mv.addObject("exception","web_file1");

in your view retrieve data by the key ie exception ${exception}
eg : <input type="text" value="${exception}" />

2. if used spring mvc with jsp/html Ensure you have declared  a bean for viewResolver
which has following format

  

      <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
                <value>abc.jsp</value>
            </property>
        </bean>
        model.addAttribute("exception", "web_file2")

        String sModel=model.toString();  //{exception=web_file2}
        String returnView = (sModel).substring(11,sModel.length()-1);  //web_file2
        return new ModelAndView(returnView);    

我找到了获取它的方法,

但我认为有更好的方法来做到这一点。