【问题标题】:dispatcherservlet does not wire model to viewdispatcherservlet 不将模型连接到视图
【发布时间】:2012-05-22 05:21:37
【问题描述】:

我从控制器创建了一个模型并返回到 dispatcherservlet。好像没有问题

在模型中,因为我仔细检查了 system.out.println 的输出。对于 viewname 字符串,我仔细检查了

一个实际的目录名称,即“WEB-INF/views/hello.jsp”。

但我相信调度程序 servlet 不会使模型适应视图,因为浏览器不显示

应该显示的模型值。更容易理解我正在经历的事情

如果我把我的代码放在这里。所以...这是我的代码。

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/appServlet/spring-servlet.xml
    </param-value>
</context-param>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<context:annotation-config/>
<bean name="/hello" class="com.spring.toby.HelloController"/>
<bean id="HelloSpring" class="com.spring.toby.HelloSpring"></bean>  
</beans>

和控制器java文件

public class HelloController implements Controller{

@Autowired HelloSpring helloSpring;
public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    String name = request.getParameter("name");
    String msg = this.helloSpring.sayHello(name);
    System.out.println(msg);
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("msg", msg);
    return new ModelAndView("WEB-INF/views/hello.jsp", model);
}
}

和bean文件

public class HelloSpring {
public String sayHello(String name){
    return "Hello " + name;
}
}

和jsp文件

</head>
    <body>
    <div><h1>Testing</h1></div>
    ${message}
    </body>

谁能告诉我我做错了什么?提前致谢。

【问题讨论】:

  • 您得到的确切错误是什么?你能在这里显示错误控制台或附上一些屏幕截图吗?
  • no.. 我没有收到错误消息。哦..我忘了在这里附加jsp文件。如果您查看我的 jsp 文件,您就可以理解我的问题。
  • 好的,那就继续吧。请同时显示您的 jsp 代码。
  • ${message} 应该更改为 "hello" + requestparam 但看起来视图没有重新调整模型..哦等等...我有错字吗?让我检查一下。我将修复消息以命名
  • 是的,这是一个错字。请参阅下面的答案。

标签: spring


【解决方案1】:

这只是一个愚蠢的错误。

请将您的 jsp 中的 ${message} 替换为 ${msg},您将在您的 jsp 中打印出消息。

希望对你有所帮助。

干杯。

【讨论】:

  • ah.. thx.. 总是简单的错误让我花太多时间。
  • 是的...有时我也会遇到这种情况。只是因为工作时花费了过多的时间。去休息一下吧。 :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多