2017-05-09 27 views
0

我正在做第一個spring MVC應用程序。其中jsp頁面不支持jsp taglib或頁面指令或el。jsp指令和el在spring項目中不工作

我在項目中添加了所有jar文件。

我不知道我犯了什麼錯誤。

這是我examle-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:mvc="http://www.springframework.org/schema/mvc" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.3.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> 
<mvc:default-servlet-handler/> 
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
<bean name="/welcome.html" class="ray.spring.controller.HelloController"/> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
<display-name>springMVC1</display-name> 
<servlet> 
    <servlet-name>example</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>example</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

的hello.jsp頁面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> 
<head> 
<%@ page isELIgnored="false" %> 
<title>Insert title here</title> 

</head> 
<body> 
<h1>1st Spring MVC Application</h1> 
<h2>${MESSAGE }</h2> 
</body> 
</html> 

控制器類

package ray.spring.controller; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.AbstractController; 

public class HelloController extends AbstractController { 

@Override 
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { 
    // TODO Auto-generated method stub 
    ModelAndView mv = new ModelAndView("hello"); 
    mv.addObject("MESSAGE","Hey User; Welcome to 1st Spring MVC Application"); 
    return mv; 
} 
} 

這是輸出我得到

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html><head><%@ page isELIgnored="false" %><title>Insert title here</title></head> 
<body> 
<h1>1st Spring MVC Application</h1> 
<h2>${MESSAGE }</h2> 
</body> 
</html> 

回答

-1

在你的web.xml中,嘗試改變

<url-pattern>/*</url-pattern> 

<url-pattern>/</url-pattern> 

,然後測試以查看它是否有效。