Sping Boot - JSP未呈现

ej83mcc0  于 10个月前  发布在  其他
关注(0)|答案(3)|浏览(70)

我正在使用Sping Boot 编写Spring MVC应用程序。我试图显示一个文本和一个从Controller类填充的Department对象列表。当 Boot 应用程序运行时,值被填充到Controller的List变量中,页面被重定向到上述JSP。
问题是输出是这样的。Screenshot of the output
我希望这些值填充到指定的EL中。任何帮助都非常感谢。
以下是使用的代码。

部门.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);
    }

}

MVConfiguration.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;

    }

}

部门.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+"]";
    }

}

lqfhib0f

lqfhib0f1#

您的项目中是否有JSP和JSTL jar?
如果使用maven,请添加

<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>

字符串

jljoyd4f

jljoyd4f2#

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

字符串

yr9zkbsy

yr9zkbsy3#

在我的例子中,我已经交换了webapp文件夹和WEB-INF文件夹的位置,因为jsp页面被移位了。因此无法在服务器上显示。
正确的顺序是- webapp->WEB-INF->views
检查IDE中的文件夹顺序。希望能帮上忙!

相关问题