spring2注释未呈现

时间:2016-07-02 10:23:56

标签: spring annotations

我是初次使用Spring 2.5.5项目启动Java。但是有一些问题,Spring注释尚未解决。我的意思是我的JSP直接显示spring注释,如下所示:   $ {MSG}

[\ target \ pom.xml]

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo.spring</groupId>
    <artifactId>HelloWorldSpringWeb</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorldSpringWeb Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jdk.version>1.6</jdk.version>
        <spring.version>2.5.6</spring.version>
        <jstl.version>1.2</jstl.version>
        <servletapi.version>2.5</servletapi.version>        
    </properties>

    <dependencies>

        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>

        <!-- Spring MVC framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- for compile only, your container should have this -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servletapi.version}</version>
            <scope>provided</scope>
        </dependency>       

    </dependencies>

  <build>
    <finalName>HelloWorldSpringWeb</finalName>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.11.v20150529</version>
            <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <webApp>
                    <contextPath>/spring2</contextPath>
                </webApp>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
                <wtpversion>2.0</wtpversion>
                <wtpContextName>spring2</wtpContextName>
            </configuration>
        </plugin>

      </plugins>    
  </build>
</project>

[\ WEB-INF \ web.xml]

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

[\ WEB-INF \ mvc-dispatcher-servlet.xml]

<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean name="/welcome.htm" 
        class="com.demo.spring.HelloWorldController" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <context:component-scan base-package="com.demo.spring" />

</beans>

[com.demo.spring.HelloWorldController.java]

package com.demo.spring;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/welcome")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("index");
        model.addObject("msg", "hello world");

        return model;
    }

}

[\ WEB-INF \ pages \ index.jsp]

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<body>
  <h2>Hello World!!!</h2>
  <h2>${msg}</h2>
</body>
</html>

要访问控制器,请使用以下URL:

http://127.0.0.1:8080/HelloWorldSpringWeb/welcome.htm

最后,在Web浏览器上输出如下:

Hello World!!!
${msg}

如您所见,$ {msg}未呈现。怎么了?

非常感谢。

1 个答案:

答案 0 :(得分:0)

在我调查之后,我应该纠正我的问题:&#34; Spring 2.5.6 EL尚未解决&#34;。

现在我知道我可以在其中包含EL表达式的每个页面上添加此语句:

<%@ page isELIgnored="false" %>

是的,它可以解决我的问题。但我不想在我的项目中在太多页面中添加此语句。

我发现它是由isELIgnored引起的,默认为&#34; true&#34;如果web.xml等于或低于2.3版。因此,我需要添加此代码以将isELIgnored设置为true:

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <el-ignored>false</el-ignored>
  </jsp-property-group>
</jsp-config> 

但没有运气。 EL还没有得到解决。

有人可以帮忙吗?

非常感谢。