Spring Boot - JSP不呈现

时间:2017-07-22 14:06:28

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

我正在使用Spring启动编写Spring MVC应用程序。我试图显示一个文本和一个从Controller类填充的Department对象列表。当运行启动应用程序时,将在Controller的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+"]";
    }


}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以尝试在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>