【问题标题】:EL not working in Spring MVC [duplicate]EL 在 Spring MVC 中不起作用 [重复]
【发布时间】:2018-02-11 23:17:00
【问题描述】:

我正在使用 Maven 和 Spring,而我的 EL 似乎没有在我的 JSP 上工作。结果我看到了这个。

JSP 工作,GET 和 POST 方法似乎运行良好。它似乎无法识别 EL。我也尝试过使用 JSTL,但是 EL 和 JSTL 表达式都被读取为静态文本。我一直在尝试我能找到的所有依赖项组合,但无济于事。如果有人之前遇到过这个问题,请帮忙。

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test-springboot-mvc</groupId>
<artifactId>test-springboot-mvc</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springboot.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Application.java:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

配置文件

package mvc.configuration;

import {...}

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");

        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

控制器

@Controller
public class FormController {

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String formGet(Model model) {
        model.addAttribute("id2", "ID:");
        model.addAttribute("unBoundTextBox", "Initial Value");
        return "form";
    }

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String formPost(@RequestParam("unBoundTextBox") String unBoundTextValue) {
        System.out.println("POSTBACK SUBMIT textbox value = " + unBoundTextValue);

        return "form";
    }
}

JSP:

<h2>new form</h2>
{id2}
<form action="/form" method="post">
    <input type="text" name="unBoundTextBox" value="${unBoundTextBox}" />
    <button>Submit</button>
</form>

【问题讨论】:

  • jstl是一个标签库,但是表单没有使用jstl。
  • Alex,${xxx} 的东西被称为 EL,而不是 JSTL。 JSTL 是那些&lt;c:xxx&gt; 标签。
  • @BalusC 谢谢,你是对的。但是,我看到了类似的问题/答案,但并没有解决 EL 不起作用的原因。我也没有尝试过任何实际的 JSTL。但事实证明,我必须添加 javax.servlet-api 依赖项才能使其工作。我正在使用 Maven 3.5.2 javax.servletjavax.servlet-api3.1.0provided依赖>

标签: jsp spring-mvc el


【解决方案1】:

你的jsp页面中是否包含了标签

%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多