【发布时间】:2013-10-05 05:42:38
【问题描述】:
我在 Spring MVC 中遇到问题。我无法将 JSP 作为服务器上的视图运行... 我上传了所有库,但在服务器上运行视图时出现以下错误:
2013 年 9 月 29 日上午 10:53:22 org.apache.catalina.core.AprLifecycleListener 初始化 信息:在 java.library.path: C:\long path.... 上找不到允许在生产环境中获得最佳性能的基于 APR 的 Apache Tomcat Native 库。
[SetContextPropertiesRule]{Context} 将属性 'source' 设置为 >'org.eclipse.jst.jee.server:SpringMVC' 没有找到匹配的属性。
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="Controllers" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
如下控制器名称为HelloController.java
package Controllers;
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";
}
}
如下图一个视图名是hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
【问题讨论】:
-
你遇到了什么问题?页面没有加载到浏览器?你如何返回
Views? APR 库对 JSP 渲染没有影响,它只是使用原生连接器来提高性能。 -
请出示您的控制器。
-
我知道这很傻,但是你的 hello.jsp 位于哪里?
-
@DirkLachowski Lachowski 我添加了控制器和视图
-
@blackpanther hello.jsp 在子文件夹路径 = /WebContent/WEB-INF/jsp/hello.jsp
标签: java eclipse spring jsp spring-mvc