【问题标题】:Why is pageContext.request.contextPath interpreted as $%7BpageContext.request.contextPath%7D?为什么 pageContext.request.contextPath 被解释为 $%7BpageContext.request.contextPath%7D?
【发布时间】:2020-01-04 06:36:53
【问题描述】:

我正在使用纯 Java 配置编写一个基于 REST 的简单 Spring MVC 应用程序。我以前写过很多应用程序,但从来没有遇到过这个问题。 我的网址<a href="${pageContext.request.contextPath}/test/hello">Hello</a> 被解释为 $%7BpageContext.request.contextPath%7D/test/hello

在研究中,我发现我需要将 web-app 版本设置为 2.4+ 当我正在编写一个纯 java 配置应用程序时,我无法在 web.xml 中更改它。 所以我尝试将动态 Web 模块更改为 3.0(当前设置为 2.3)。 但是,由于很多人都面临过这个问题,eclipse 不允许我改变它。 所以我在 project.facet.core.xml 中更改了它,但它没有解决 url 问题,而且我收到一个新错误“无法将项目 facet 动态 Web 模块的版本更改为 2.3”。 我不明白的是,如果我在 xml 中手动将 web 模块版本设置为 3.0,为什么 eclipse 试图再次设置为 2.3?

呜呜!!!很多问题 我已经尝试解决这个问题大约 8 小时,但没有任何成功,所以请在这件事上提供任何帮助!

jsp文件:

<html>
<body>
<h2>Hello World!</h2>
<a href="mavn_spring_rest_demo/test/hello">try it </a>
</body>
</html>

配置类:

```@EnableWebMvc
```@ComponentScan("com.org")
```public class MyConfig implements WebMvcConfigurer{
```}

servlet initializer class:
```public class MyServletInitializerConfig extends  
```AbstractAnnotationConfigDispatcherServletInitializer {
    ```@Override
    ```protected Class<?>[] getServletConfigClasses() {
        ```return new Class[] { MyConfig.class };
    ```}
    ```@Override
    ```protected String[] getServletMappings() {
        ```return new String[] { "/" };
    ```}

rest controller:
```@RestController
```@RequestMapping("/test")
```public class MyRestController {
    ```@GetMapping("/hello")
    ```public String sayHello() {
           return "hello finally";
    ```}
```}

【问题讨论】:

  • 虽然不清楚你的表达为什么没有得到解决,但我只是建议写${request.contextPath}。此处无需遍历 pageContext 对象。
  • 感谢@Mick 的回复,但现在是$%7Brequest.contextPath%7D
  • JSP 处理器似乎没有启动。请发布您的完整 JSP 源代码。尤其是标题行。
  • @Mick 使用完整的 jsp 文件更新了操作。这是默认创建的基本的,我刚刚添加了我的网址

标签: java spring spring-mvc jsp


【解决方案1】:

你应该删除你的 web.xml 文件并重新启动你的本地服务器,我认为你和我遵循相同的过程。我遇到了这个问题,因为我有一个空的 web.xml 文件。我是一个新手,但是当只配置普通的 java 类时,空的 web.xml 文件会妨碍我。

试试看吧。

【讨论】:

    【解决方案2】:

    当我在我的 jsp 文件中使用 {pageContext.request.contextPath} 时,我遇到了同样的问题,正如你所说的问题是(2.3 的 web 应用程序),我通过修改 web.xml 中的 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" 
             version="3.1">
      <display-name>spring-rest-demo</display-name>
    </web-app> 
    

    您可以将此代码复制到您的web.xml 文件中,

    希望这对您的项目有用

    【讨论】:

      【解决方案3】:

      只需在您的 jsp 文件顶部添加以下 JSP 指令:

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
      

      JSP 指令用于向 JSP 容器提供特殊指令,以将 JSP 转换为 servlet 代码。

      https://www.guru99.com/jsp-actions.html了解更多关于 JSP 指令的信息

      【讨论】:

        猜你喜欢
        • 2010-12-20
        • 2020-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 2013-07-08
        • 2018-10-12
        • 2015-09-21
        相关资源
        最近更新 更多