如何访问模型属性的值并将其设置给ModelAndView。

huangapple go评论72阅读模式
英文:

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

问题

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;
英文:

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;

答案1

得分: 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);

我已找到一种方法来实现这个,

但我认为有一种更好的方法来做这件事。

英文:
        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.

答案2

得分: 0

  1. 在视图中使用以下代码放置您的数据:
mv.addObject("exception", "web_file1");

在视图中通过键检索数据,例如:${exception}
例如:<input type="text" value="${exception}" />

  1. 如果使用Spring MVC与JSP/HTML,请确保已声明一个用于viewResolver的bean,其格式如下:
<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>
英文:
1.  put your  data using 
mv.addObject(&quot;exception&quot;,&quot;web_file1&quot;);

in your view retrieve data by the key ie exception ${exception}
eg : &lt;input type=&quot;text&quot; value=&quot;${exception}&quot; /&gt;

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

  

      &lt;bean id=&quot;viewResolver&quot;
        	class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
            &lt;property name=&quot;prefix&quot;&gt;
                &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
            &lt;/property&gt;
            &lt;property name=&quot;suffix&quot;&gt;
                &lt;value&gt;abc.jsp&lt;/value&gt;
            &lt;/property&gt;
        &lt;/bean&gt;

huangapple
  • 本文由 发表于 2020年8月18日 01:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63455485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定