在Spring Boot中从Controller类到JSP页面的路由问题

时间:2018-11-12 07:39:53

标签: spring-boot

        import java.util.Map;            
        import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.ResponseBody;
        import org.springframework.web.bind.annotation.RestController;
        import org.springframework.web.servlet.ModelAndView;


        @RestController
        public class EmployeeController {

            private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class); 

            @RequestMapping(value="/getEmployees")
            public String getEmployeeInfo() {
                logger.info("inside employee controller");      
               return "getEmployees";
            }

        }

<%@页面语言=“ java” contentType =“ text / html; charset = ISO-8859-1”                 pageEncoding =“ ISO-8859-1”%>             

        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Get Employees</title>
        </head>
        <body>this is employee list</body>
        </html>

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

      <groupId>com.phynart</groupId>
      <artifactId>oAuth-client</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>

      <name>oAuth-client</name>
      <url>http://maven.apache.org</url>

      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>

      <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>

      <dependencies>        
            <!-- <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
             <version>1.3.3.RELEASE</version>
            </dependency> -->   
             <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>    
                <!-- <scope>provided</scope> -->        
            </dependency>
            <dependency>
                <groupId>org.eclipse.jdt.core.compiler</groupId>
                <artifactId>ecj</artifactId>
                <version>4.6.1</version>
                <!-- <scope>provided</scope> -->
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <!-- <scope>provided</scope> -->
            </dependency>
            <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter</artifactId>
                </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
                <version>1.4.5.RELEASE</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-s3</artifactId>
                <version>1.11.113</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ses -->
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-ses</artifactId>
                <version>1.11.114</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-sns -->
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-sns</artifactId>
                <version>1.11.115</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-admin -->
            <dependency>
                <groupId>com.google.firebase</groupId>
                <artifactId>firebase-admin</artifactId>
                <version>4.1.7</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.3</version><!--$NO-MVN-MAN-VER$ -->
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>
                    spring-boot-starter-cloud-connectors
                </artifactId>
            </dependency>
            <dependency>
                <groupId>org.passay</groupId>
                <artifactId>passay</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>org.hashids</groupId>
                <artifactId>hashids</artifactId>
                <version>1.0.3</version>
            </dependency>   
            <dependency>
                <groupId>org.springframework.security.oauth</groupId>
                <artifactId>spring-security-oauth2</artifactId>
                </dependency>
            <dependency>
                 <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

      </dependencies>
    </project>           


    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;

    @SpringBootApplication
    public class SpringBootFormHandingApplication extends SpringBootServletInitializer {

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

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringBootFormHandingApplication.class);
        }
    }




    spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
    server.port:8081
    spring.mvc.view.prefix: /WEB-INF/jsp/
    spring.mvc.view.suffix: .jsp
    security.basic.enabled=false

在端口8081上击中URL“ / getEmployees”时,我没有得到字符串“ this is employee list”作为响应,而是得到了字符串“ getEmployees”         从Controller类返回,这意味着控制器没有重定向到应该打印“这是员工列表”的jsp页面。...请有人帮我吗?对我来说要花很多时间。...

project Structure

1 个答案:

答案 0 :(得分:0)

@RestController:是@Controller@RestponseBody的组合。因此,从REST控制器方法返回的所有内容都将以String({JSON/XML)的形式返回,因为这是REST Full Web Services所期望的。

@Controller:创建模型对象的Map并查找返回值的视图。

因此,从@RestController更改为@Controller应该会对您有所帮助。

是的,需要在Application类上使用@EnableWebMvc