jsp指令和el不在spring项目中工作

时间:2017-05-09 04:42:33

标签: jsp spring-mvc

我正在做第一个春季MVC应用程序。其中jsp页面不支持jsp taglib或page directive或el。

我在项目中添加了所有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 个答案:

答案 0 :(得分:-1)

在您的web.xml中,尝试更改

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

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

然后测试它是否有效。