2017-05-03 7 views
0

私は簡単なSpringブートMVCアプリケーションを開発しています。以前は、Spring + Hibernate(つまりservlet.xmlなど)だけで作業していましたが、SpringBootを使用するプロジェクトを再構築しようとしています。しかし、私はまだ多くの答えを読んだが、私はまだ私の.jspファイルを見つけることができない春のブートを修正することはできません - それはwhitelabelのエラーページを表示します。
多くの回答はtomcat-embed-jasperjstlpom.xmlに含めていますが、すでに持っています。私はIntelliJ IDEA 17 Ultimate、Tomcat 8.5.13、SpringとSpring Bootの最新バージョンを使用しています。Springブートjspファイルが見つかりません

プロジェクト構造:
enter image description here

の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> 
<artifactId>spring-invoice</artifactId> 
<groupId>spring-invoice</groupId> 
<version>1.0.0</version> 
<packaging>war</packaging> 

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

<properties> 
    <springframework.version>4.3.8.RELEASE</springframework.version> 
    <hibernate.version>5.2.9.Final</hibernate.version> 
    <mysql.connector.version>5.1.41</mysql.connector.version> 
    <hibernatevalidator.version>5.4.1.Final</hibernatevalidator.version> 
    <start-class>com.invoices.Application</start-class> 
</properties> 

<dependencies> 

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

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

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

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

    <!-- Spring --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>${springframework.version}</version> 
    </dependency> 

    <!-- Hibernate --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>${hibernate.version}</version> 
    </dependency> 

    <!-- Hibernate validator --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-validator</artifactId> 
     <version>${hibernatevalidator.version}</version> 
    </dependency> 

    <!-- MySQL --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>${mysql.connector.version}</version> 
    </dependency> 

    <!-- C3PO --> 
    <dependency> 
     <groupId>com.mchange</groupId> 
     <artifactId>c3p0</artifactId> 
     <version>0.9.5.2</version> 
    </dependency> 

    <!-- Servlet + JSP + JSTL --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>javax.servlet.jsp-api</artifactId> 
     <version>2.3.1</version> 
    </dependency> 

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

</dependencies> 

<build> 

    <finalName>spring-invoice</finalName> 

    <plugins> 

     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 

     <!-- The Compiler Plugin is used to compile the sources of project --> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.6.0</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 

     <!-- Builds a Web Application Archive (WAR) file from the project output and its dependencies. --> 
     <plugin> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>3.0.0</version> 
      <configuration> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 

    </plugins> 

</build> 

</project> 

application.propertiesファイル

# Database 
spring.datasource.url=jdbc:mysql://localhost:3306/fakturowanie 
spring.datasource.username=root 
spring.datasource.password=1234 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect 

# Hibernate 
spring.jpa.hibernate.ddl-auto=update 
spring.jpa.show-sql=true 

entitymanager.packagesToScan=com.invoices 
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy 

spring.mvc.view.prefix=/WEB-INF/view/ 
spring.mvc.view.suffix=.jsp 

Applicationクラス

@SpringBootApplication 
public class Application extends SpringBootServletInitializer 
{ 
    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
    { 
     return application.sources(Application.class); 
    } 

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

私のJSPファイルの1つ

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
 
<html lang="pl-PL"> 
 
<head> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
 
    <title>Zarządzaj kontrahentami</title> 
 
</head> 
 
<body> 
 

 
    <div id="wrapper"> 
 
     <div id="header"> 
 
      <h2>Zarządzaj kontrahentami</h2> 
 
     </div> 
 
    </div> 
 

 
    <div> 
 
     <div> 
 
      <!-- put new button: Add Customer --> 
 
      <input type="button" value="Dodaj nowego kontrahenta" 
 
        onclick="window.location.href='addCustomer'; return false;" 
 
        class="add-button"/> 
 

 
      <table> 
 
       <tr> 
 
        <th>Alias</th> 
 
        <th>Nazwisko</th> 
 
        <th>Imię</th> 
 
        <th>Nazwa firmy</th> 
 
        <th>NIP/PESEL</th> 
 
        <th>Ulica i nr mieszkania</th> 
 
        <th>Kod pocztowy</th> 
 
        <th>Miejscowość</th> 
 
        <th>Sposób zapłaty</th> 
 
        <th>Uwzględnij numer faktury</th> 
 
       </tr> 
 

 
       <c:forEach var="tempCustomer" items="${allCustomers}"> 
 

 
        <c:url var="updateLink" value="/customers/updateCustomer"> 
 
         <c:param name="customerID" value="${tempCustomer.id}"/> 
 
        </c:url> 
 

 
        <c:url var="deleteLink" value="/customers/deleteCustomer"> 
 
         <c:param name="customerID" value="${tempCustomer.id}"/> 
 
        </c:url> 
 

 
        <tr> 
 
         <td>${tempCustomer.alias}</td> 
 
         <td>${tempCustomer.lastName}</td> 
 
         <td>${tempCustomer.firstName}</td> 
 
         <td>${tempCustomer.companyName}</td> 
 
         <td>${tempCustomer.taxIdentifier}</td> 
 
         <td>${tempCustomer.postalCode}</td> 
 
         <td>${tempCustomer.city}</td> 
 
         <td>${tempCustomer.paymentMethod}</td> 
 
         <td>${tempCustomer.includeInCount}</td> 
 

 
         <td> 
 
          <!-- display the update and delete link --> 
 
          <a href="${updateLink}">Edytuj</a> 
 
          | 
 
          <a href="${deleteLink}" onclick="if (!(confirm(
 
           'Czy na pewno usunąć tego kontrahenta?'))) return false">Usuń</a> 
 
         </td> 
 

 
        </tr> 
 

 
       </c:forEach> 
 

 
      </table> 
 

 
     </div> 
 
    </div> 
 

 
</body> 
 
</html>

そして、それのコントローラー

@Controller 
@RequestMapping("/customers") 
public class CustomersController implements IUtilities<Customer> 
{ 
    @Autowired 
    private ICustomerDAO customerDAO; 

    @GetMapping("/manageCustomers") 
    public String manageCustomers(Model model) 
    { 
     List<Customer> allCustomers = convertIterableToCollection(customerDAO.findAll()); 
     model.addAttribute("allCustomers", allCustomers); 
     return "manage-customers"; 
    } 

    @GetMapping("/addCustomer") 
    public String showFormForAdd(Model model) 
    { 
     // create model attribute to bind form data 
     Customer theCustomer = new Customer(); 

     model.addAttribute("customerToEdit", theCustomer); 

     return "customer-form"; 
    } 

    @GetMapping("/updateCustomer") 
    public String showFormForUpdate(@RequestParam("customerID") int customerId, Model model) 
    { 
     // get the customer from service 
     Customer theCustomer = customerDAO.findOne(customerId); 
     model.addAttribute("customerToEdit", theCustomer); 

     //redirect to update form 
     return "customer-form"; 
    } 

    @GetMapping("/deleteCustomer") 
    public String deleteCustomer(@RequestParam("customerID") int customerId) 
    { 
     //delete the customer 
     customerDAO.delete(customerId); 

     return "redirect:/customers/manageCustomers"; 
    } 

    @PostMapping("/saveCustomer") 
    public String saveCustomer(@ModelAttribute("customerToEdit") Customer customer) { 
     //save the customer using our service 
     customerDAO.save(customer); 

     return "redirect:/customers/manageCustomers"; 
    } 

    @Override 
    public List<Customer> convertIterableToCollection(Iterable<Customer> iterable) { 
     List<Customer> list = new ArrayList<>(); 
     iterable.forEach(list::add); 
     return list; 
    } 
} 
+0

ここで、viewResolverのBeanを追加しますか –

+0

私のプロジェクトではviewResolverのようなものはありません。xmlファイルも設定クラスもありません。 – Colonder

+0

あなたの基本的な問題は予想されるmavenプロジェクトレイアウトを使用していません。 –

答えて

2

あなたは春のブートを使用しているが、あなたのポンポンは、競合の依存関係だらけています。初心者の方はpom.xmlをクリーンアップしてください。また、あなたのポンポンでは、少なくともその代わり、デフォルトcompile範囲のspring-boot-starter-tomcatprovidedを作る意味はどの(JSP Samples pom.xmlに行われているものを考慮に入れる必要があります。

を最後にあなたがそれらとあるmaven-compilemaven-warプラグインを必要としませんすでに親から継承された。

<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> 
<artifactId>spring-invoice</artifactId> 
<groupId>spring-invoice</groupId> 
<version>1.0.0</version> 
<packaging>war</packaging> 

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

<properties> 
    <hibernate.version>5.2.9.Final</hibernate.version> 
    <start-class>com.invoices.Application</start-class> 
</properties> 

<dependencies> 

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

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

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-tomcat</artifactId> 
     <scope>provided</scope> 
    </dependency> 

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

    <!-- MySQL --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
    </dependency> 

    <!-- C3PO --> 
    <dependency> 
     <groupId>com.mchange</groupId> 
     <artifactId>c3p0</artifactId> 
     <version>0.9.5.2</version> 
    </dependency> 

    <!-- Servlet + JSP + JSTL --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
    </dependency> 

</dependencies> 

<build> 

    <finalName>spring-invoice</finalName> 

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

</build> 

</project> 

また、JSPをして春のブートを使用している場合があります考慮the limitationsに取る。実際の問題は、あなたがusinされ、プロジェクト構造であるしかし

g Mavenにはsrc/main/javaディレクトリがあり、Webソースにはwebの代わりにsrc/main/webapp、さらにJava以外の関連リソースはsrc/main/resourcesでなければなりません。このデフォルト構造を持たないため、ファイルは正しい場所にコピーされません(実際には無視されます)。

+0

私は以前のバージョンのプロジェクトから 'pom.xml'を継承しました - 私はそれをきれいにして、それが動作しているかどうかを確認しようとします – Colonder

+0

修正された答えを見てください(実際の問​​題はあなたのプロジェクト構造です)。いいえ、あなたのポンを掃除することで、それ以上の問題(およびメンテナンス)を防ぐことができます。 –

+0

それは私が正直だと思ったことですが、それでも、私は新しいプロジェクトを作成するときにIntelliJが私に与えるものです。これは、Spring BootとMavenを使って作業するたびに、プロジェクト構造を手動で毎回再構築する必要があることを意味しますか? – Colonder

関連する問題