ModelMap/ModelAndView数据未显示
ModelMap/ModelAndView data is not displayed
我正在尝试 运行 spring mvc helloworld 示例 tutorialspoint.but I am not geeting the modelmap data in jsp page .I had added all the required jars in web-inf/lib folder.I am also using <%@ page contentType="text/html; charset=UTF-8" isELIgnored="false" %>
.But still i am not getting data in jsp page. i also view so many answers for this question on Whosebug and on http://www.mkyong.com/spring-mvc/modelandviews-model-value-is-not-displayed-in-jsp-via-el/
这是我的项目flow.Please帮助提前致谢
.
我的 HelloController 是
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
web.xml 是 ::
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml 是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是我的 hello.jsp::
<%@ page contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
hiiiii <h2>${message}</h2>
</body>
</html>
通常应用程序会查找文件夹 webapp
(位于 src/main/webapp/
),您应该将整个 WEB-INF 文件夹与相应的 jsp
文件一起移动(src/main/webapp/WEB-INF/jsp/hello.jsp
).
但是您使用的教程 (tutorialspoint
) 假定将整个 WEB-INF 复制到 tomcat 中的 webapp 文件夹 - 以及相应的库 (
Once you are done with creating source and configuration files, export your application. Right click on your application and use Export > WAR File option and save your HelloWeb.war file in Tomcat's webapps folder.
对于未来,我强烈建议使用像 maven 这样的构建自动化工具,这将简化过程 spring mvc with maven。
将 web.xml xsd 架构从 2.4 更改为 3,例如:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="true" version="3.0">
我解决了这个问题。我 made.Thanks @akfaz 的结构有一些问题..我们必须在 web-inf 中创建一个 jsp 文件夹然后必须为此代码编写 hello.jsp...这是一个我当前项目流程的屏幕排序工作正常,我能够使用 spring MVC 获取 ModelMap/Model 的数据并在 jsp 中查看。:)
我正在尝试 运行 spring mvc helloworld 示例 tutorialspoint.but I am not geeting the modelmap data in jsp page .I had added all the required jars in web-inf/lib folder.I am also using <%@ page contentType="text/html; charset=UTF-8" isELIgnored="false" %>
.But still i am not getting data in jsp page. i also view so many answers for this question on Whosebug and on http://www.mkyong.com/spring-mvc/modelandviews-model-value-is-not-displayed-in-jsp-via-el/
这是我的项目flow.Please帮助提前致谢
.
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
web.xml 是 ::
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml 是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是我的 hello.jsp::
<%@ page contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
hiiiii <h2>${message}</h2>
</body>
</html>
通常应用程序会查找文件夹 webapp
(位于 src/main/webapp/
),您应该将整个 WEB-INF 文件夹与相应的 jsp
文件一起移动(src/main/webapp/WEB-INF/jsp/hello.jsp
).
但是您使用的教程 (tutorialspoint
) 假定将整个 WEB-INF 复制到 tomcat 中的 webapp 文件夹 - 以及相应的库 (
Once you are done with creating source and configuration files, export your application. Right click on your application and use Export > WAR File option and save your HelloWeb.war file in Tomcat's webapps folder.
对于未来,我强烈建议使用像 maven 这样的构建自动化工具,这将简化过程 spring mvc with maven。
将 web.xml xsd 架构从 2.4 更改为 3,例如:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="true" version="3.0">
我解决了这个问题。我 made.Thanks @akfaz 的结构有一些问题..我们必须在 web-inf 中创建一个 jsp 文件夹然后必须为此代码编写 hello.jsp...这是一个我当前项目流程的屏幕排序工作正常,我能够使用 spring MVC 获取 ModelMap/Model 的数据并在 jsp 中查看。:)