繁体   English   中英

JSP无法通过Spring MVC进行评估

[英]JSP not Evaluating with Spring MVC

我正在尝试使用注释驱动的配置在Spring 3.2中呈现JSP,但是JSP呈现为字符串,并且不进行评估。

我正在使用Maven Jetty插件在开发中运行Webapp。 因此,似乎一切都应该“正常工作”。

我包括使用JSP的依赖项是

<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

用于配置JSP的bean是

@Configuration
public class WebAppConfiguration {
  @Bean
  public InternalResourceViewResolver internalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/views/");
    resolver.setSuffix(".jsp");
    return resolver;
  }
}

控制器非常简单

@Controller
public class RootController {
  @RequestMapping(value = "/login")
  public String login() {
    return "login";
  }

而且JSP也很容易

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>

<html>
<head></head>
<body>
<%= "Hello World" %>
${ "Hello World" }
<form name="auth" action="<c:url value='j_spring_security_check' />" method="POST">
  <label>Username: <input type="text" name="j_username"></label>
  <label>Password: <input type="password" name="j_password"></label>
  <input type="submit" value="Submit"/>
</form>
</body>
</html>

从图像中可以看到,JSP没有被评估。 我需要做些什么来告诉JSP在呈现时进行评估。

渲染页面

编辑1

因此,仅需一点点额外信息,我就使用了Resthub原型resthub-mongodb-backbonejs-archetype来引导该项目,该项目使用WebAppInitializer而不是较旧的web.xml,并且它使用新的注释驱动的bean而不是xml bean。


编辑2

我一直在这方面砸了很长时间,所以我将项目放在github https://github.com/austinbv/calendar/上 因为我不知道什么重要,什么不重要。

谢谢您的帮助


使用spring boot时,我遇到了同样的问题。 将这些依赖项添加到项目pom.xml中可以解决此问题:

<dependency>
    <groupId>tomcat</groupId>
    <artifactId>jasper-compiler</artifactId>
    <version>5.5.23</version>
</dependency>
<dependency>
    <groupId>tomcat</groupId>
    <artifactId>jasper-runtime</artifactId>
    <version>5.5.23</version>
</dependency>
<dependency>
    <groupId>tomcat</groupId>
    <artifactId>jasper-compiler-jdt</artifactId>
    <version>5.5.23</version>
</dependency>

@austinbv请使用SPRING LINK检查设置。 (正如@Rohit在上面指出您的内容-丢失的部分)

在“ web.xml”中进行以下更改后,为我解决了上述给定问题

spring servlet必须是默认的servlet。 即映射到/而不是/ *。

引用链接: https : //code-examples.net/en/q/b49ce1

您需要指定适当的视图类

 public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
resolver.setPrefix("/views/");
resolver.setSuffix(".jsp");
return resolver;

}

我不知道实际的答案是多少,但是我遇到了完全相同的问题(Spring + boot + maven + tomcat)。 我通过删除tomcat.embed依赖项提供的范围解决了该问题。 所以,我的依赖现在看起来像这样:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

因为JSP不遵循MVC模式:P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM