0

I cant seem to find the solution for this.

I have method that returns ModelAndView to view web pages.

I store a webpage filename to model whenever a condition is true.

Example:

    @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")
        }
          
            

How can I retrieve the data stored in "exception" and set it to ModelAndView?

                ModelAndView mav = new ModelAndView();
                mav.setViewName("exception");  
                //expected:web_file2 
                //actual:exception
                return mav;
        
Aya Sato
  • 29
  • 1
  • 9

2 Answers2

1
        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);    

I've found a way to get it,

But I think there's a better way to do this.

Aya Sato
  • 29
  • 1
  • 9
0
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>
  • Thanks, but I need to retrieve the data in my java controller(java), not in the view (jsp/html). If its possible – Aya Sato Aug 17 '20 at 19:22
  • is "exception" any html/jsp , how you are calling the url /process from ?? you need to send that data from that view itself, and bind in controller by @RequestParam/@Pathvariable – Avinash Mishra Aug 17 '20 at 19:35
  • `exception` is not the file name, I used `Model` to store the value to `exception` : `model.addAttribute("exception", "web_file2")`, I don't think I need to send the data through view because I also have other filenames for every condition I have. – Aya Sato Aug 17 '20 at 20:05