【问题标题】:Spring Boot - JSP is not renderingSpring Boot - JSP 未呈现
【发布时间】:2017-07-22 14:06:28
【问题描述】:

我正在使用 Spring Boot 编写 Spring MVC 应用程序。我正在尝试显示从 Controller 类填充的文本和 Department 对象列表。当启动应用程序运行时,这些值被填充到控制器的 List 变量中,并且页面被重定向到提到的 JSP。

问题是输出是这样的。 Screenshot of the output

我希望将值填充到指定的 EL 中。很感谢任何形式的帮助。

以下是使用的代码。

Department.jsp

<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
</head>
<body>
    <div>
        <div>
            <h1>${message}</h1>
                <ul>
                <c:forEach var="listValue" items="${DepartmentList}" >
                      <li>${listValue.deptName}</li>
                 </c:forEach>
                 </ul>
        </div>
    </div>
</body>
</html>

SpringMVCApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringMVCApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringMVCApplication.class);
    }

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

}

MVCConfiguration.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration
@EnableWebMvc
@ComponentScan(value = "com.practices.spring.mvc")
public class MVCConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setContentType("application/html");

        registry.viewResolver(resolver);
    }

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


}

DepartmentController.java

import java.util.ArrayList;
import java.util.List;

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

import com.practices.spring.mvc.valueobject.Department;

@Controller
public class DepartmentController {


    @RequestMapping(method = RequestMethod.GET, value = "/depts")
    public ModelAndView getAllDepartments(){
        ModelAndView mv = new ModelAndView("Department");
        List<Department> deptList = populateDepartment();

        mv.addObject("DepartmentList", deptList);
        mv.addObject("message", "Hi");


        return mv;
    }




    private List<Department> populateDepartment(){
        List<Department> deptListTemp = new ArrayList<>();
        Department dept1 = new Department("1", "Finance");
        Department dept2 = new Department("2", "CRO");
        deptListTemp.add(dept1);
        deptListTemp.add(dept2);

        return deptListTemp;

    }

}

Department.java

package com.practices.spring.mvc.valueobject;

public class Department {
    private String deptId;
    private String deptName;

    public Department(){}

    public Department (String deptIdentification, String deptartmentName){
        deptId = deptIdentification;
        deptName = deptartmentName;
    }

    public String getDeptId() {
        return deptId;
    }
    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }


    public String toString(){
        return "[deptId : "+deptId +" ! deptName : "+deptName+"]";
    }


}

【问题讨论】:

    标签: java spring jsp spring-mvc spring-boot


    【解决方案1】:

    您的项目中是否有 JSP 和 JSTL jar?

    如果你使用maven add

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>1.2.1</version>
    </dependency>
    

    【讨论】:

    • 没有帮助.. 仍然没有响应
    【解决方案2】:

    您可以尝试在pom.xml 中添加以下依赖项吗?

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>provided</scope>
    </dependency>
    

    【讨论】:

    • 添加 Maven 条目后,它工作正常。非常感谢
    • 没有什么对我有用@Arpit Aggarwal
    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 2021-01-05
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多