2

我正在尝试使用注释驱动配置在 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/上。因为我不知道什么重要什么不重要。

谢谢您的帮助


4

6 回答 6

1

@austinbv Please use the SPRING LINK to check the setup. (As @Rohit has pointed you above - the missing piece)

于 2013-02-17T00:56:47.737 回答
1

我在使用弹簧靴时遇到了同样的问题。将这些依赖项添加到项目 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>
于 2016-03-26T03:53:06.010 回答
1

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

spring servlet 需要是默认的 servlet。即映射到/而不是/*。

参考链接:https ://code-examples.net/en/q/b49ce1

于 2019-08-13T04:11:10.867 回答
0

我不知道我的答案会有多少实际,但我遇到了完全相同的问题(Spring + boot + maven + tomcat)。我通过从 tomcat.embed 依赖中删除 scope-provided 来解决它。所以,我的依赖现在看起来像这样:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
于 2015-03-05T06:58:06.923 回答
0

您需要指定适当的视图类

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

}

于 2013-02-16T23:52:04.387 回答
0

因为 JSP 不遵循 MVC 模式:P

于 2017-06-08T06:26:33.140 回答