Gradle-GET /WEB-INF/jsp/welcome.jsp没有映射“

时间:2019-02-11 21:34:12

标签: spring-boot jsp gradle

我正在尝试使用spring-boot和gradle创建一个简单的CRUD应用程序作为PoC。此时,我在尝试加载前端网站时收到此错误-

2019-02-11 14:54:48.915 WARN 5704 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound : No mapping for GET /WEB-INF/jsp/welcome.jsp

我通过在控制器类,应用程序属性和构建gradle脚本中进行指定来使用spring boot和gradle,但是当我转到我的welcome.jsp和现在对应的404错误时,仍然得到此调试语句。

我已经看过很多教程,但是其中很多都是行家。至此,我知道我需要在控制器中指定路径,并在属性和构建脚本中指定资源。我什至尝试创建一个特定的mvcconfiguration,但我认为我不需要一个。

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }} 
  apply plugin: 'eclipse' 
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.bill'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudServicesVersion', '2.1.0.RELEASE')
    set('springCloudVersion', 'Greenwich.RELEASE')
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-actuator'

    /*implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-config-client'
    /*implementation 'io.pivotal.spring.cloud:spring-cloud-services-starter-service-registry'
    implementation 'org.springframework.cloud:spring-cloud-starter'
    /*implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-ribbon'
    */
  runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "io.pivotal.spring.cloud:spring-cloud-services-dependencies:${springCloudServicesVersion}"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

application.properties

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

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/mh1
spring.datasource.username=root
spring.datasource.password=root

MainController.java

package com.javainuse;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.javainuse.BankAccountApplication;
import com.javainuse.User;
import com.javainuse.UserDAO;
import com.javainuse.UserForm;

@Controller
public class MainController {
    @Autowired
    UserDAO userDAO;


    @RequestMapping("/welcome")
    public ModelAndView viewHome() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("welcome");
        return modelAndView;


    } 


    @RequestMapping("/users")
    public String viewUsers(Model model) {
        List<User> list = UserDAO.getUsers();

        model.addAttribute("users", list);

        return "usersPage";
    }

    @RequestMapping("/registerSuccessful")
    public String viewRegisterSuccessful() {
        return "registerSuccessfulPage";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String viewRegister(Model model) {
        UserForm form = new UserForm();

        model.addAttribute("userForm", form);

        return "registerPage";
    }

    public String saveRegister( Model model, //
             @ModelAttribute("userForm") @Validated UserForm userForm, //
             BindingResult result, //
             final RedirectAttributes redirectAttributes) {

          // Validate result
          if (result.hasErrors()) {
             return "registerPage";
          }
          User newUser= null;
          try {
             newUser = userDAO.createUser(userForm);
          }
          // Other error!!
          catch (Exception e) {
             model.addAttribute("errorMessage", "Error: " + e.getMessage());
             return "registerPage";
          }

          redirectAttributes.addFlashAttribute("newUser", newUser);

          return "redirect:/registerSuccessful";
       }
}

MvcConfiguration.java

package com.javainuse;

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

@Configuration
@ComponentScan(basePackages="...package name...")
@EnableWebMvc

public class MvcConfiguration extends WebMvcConfigurerAdapter{

  @Bean
  public InternalResourceViewResolver viewResolver() {

     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
     viewResolver.setPrefix("WEB-INF/jsp/");
     viewResolver.setSuffix(".jsp");

   return viewResolver;
}
}

2019-02-11 14:54:48.915 WARN 5704 --- [nio-8080-exec-3] osweb.servlet.PageNotFound:GET /WEB-INF/jsp/welcome.jsp没有映射

0 个答案:

没有答案