Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动-JSP未呈现_Java_Spring_Jsp_Spring Mvc_Spring Boot - Fatal编程技术网

Java Spring启动-JSP未呈现

Java Spring启动-JSP未呈现,java,spring,jsp,spring-mvc,spring-boot,Java,Spring,Jsp,Spring Mvc,Spring Boot,我正在使用SpringBoot编写一个SpringMVC应用程序。我试图显示一个文本和一个从Controller类填充的Department对象列表。当启动应用程序运行时,值被填充到控制器的列表变量中,页面被重定向到提到的JSP 问题是输出是这样的。 我希望在指定的EL中填充值。非常感谢您的帮助 以下是使用的代码 Department.jsp <!DOCTYPE html> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/

我正在使用SpringBoot编写一个SpringMVC应用程序。我试图显示一个文本和一个从Controller类填充的Department对象列表。当启动应用程序运行时,值被填充到控制器的列表变量中,页面被重定向到提到的JSP

问题是输出是这样的。

我希望在指定的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>
MVCConfiguration.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);
    }

}
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();
    }


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

    }

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


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

}
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();
    }


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

    }

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


}

您的项目中有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>

javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
假如
javax.servlet.jsp.jstl
javax.servlet.jsp.jstl-api
1.2.1

能否尝试在
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>

org.apache.tomcat.embed
汤姆卡特·贾斯珀
假如
javax.servlet
jstl
假如

添加Maven条目后,它运行良好。谢谢