Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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
为什么不';jsp文件使用SpringMVC与SpringBoot一起工作_Jsp_Spring Mvc_Spring Boot - Fatal编程技术网

为什么不';jsp文件使用SpringMVC与SpringBoot一起工作

为什么不';jsp文件使用SpringMVC与SpringBoot一起工作,jsp,spring-mvc,spring-boot,Jsp,Spring Mvc,Spring Boot,我花了一整天的时间试图得到一个简单的Spring启动应用程序来显示我的.jsp文件。我无法切换到其他技术(需求),也无法让它工作。这是一个非常简单的应用程序。以下是文件结构: pom.xml src/ main/ java/ hello/ HelloController.java MVCApplication.java resources/ application.properties static/

我花了一整天的时间试图得到一个简单的Spring启动应用程序来显示我的.jsp文件。我无法切换到其他技术(需求),也无法让它工作。这是一个非常简单的应用程序。以下是文件结构:

pom.xml
src/
  main/
    java/
      hello/
        HelloController.java
        MVCApplication.java
    resources/
      application.properties
      static/
        index.html
      templates/
        pages/
          helloworld.jsp
以下是我的两个java文件:

HelloController.java:

// @RestController // From when I first posted this question. 
@Controller
public class HelloController {

  // Should I add a "produces" attribute to this? I don't know what to use for jsp.
  @RequestMapping("/hello")
  public String hello(Model model) {
    model.addAttribute("greeting", "Hello Spring MVC");
    // I see this message in the output, so I know this method is getting called.
    System.err.printf("Setting \"greeting\" to \"Hello Spring MVC\"%n");
    return "helloworld";
  }
}
mvcapapplication.java:

@SpringBootApplication
public class MVCApplication extends SpringApplication {

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

  // Since I have the properties spring.mvc.view.prefix and spring.mvc.view.suffix
  // defined in applications.properties, I don't think I need this method.
  // I have tried it both with and without this method. It doesn't work either way.
  @Bean
  public ViewResolver getViewResolver() {
    // I see this line in the output, which confirms that this method gets called.
    System.err.printf("Creating custom JSP View Resolver%n"); // NON-NLS
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/templates/pages/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
  }
}
我的helloworld.jsp文件非常简单,但我从未在浏览器中看到它:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Spring MVC -HelloWorld</title>
</head>
<body>
  <h1>Greeting: ${greeting}</h1>
</body>
</html>
我知道属性正在被读取,因为我需要在浏览器中指定端口7070才能看到任何内容。所以所有这些属性都应该起作用

我的pom文件似乎具有所有正确的依赖项

    <?xml version="1.0" encoding="UTF-8"?>
    <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>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

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

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

            <!--JSP Enabled: -->
            <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>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>
        </dependencies>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <packaging>jar</packaging>

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

    </project>
它不起作用。我收到了与以前相同的错误消息

修订版。最初我使用@RestController,这是错误的。我还将spring.boot.starter.parent版本从1.5.7减少到1.5.2,更改如下:

@RestController
为此:

@Controller
您已将控制器声明为REST one,因此默认情况下,响应为“无视图”。通常,当您希望像您一样重新发送JSON序列化响应时,会使用它

您想要使用的是全栈spingmvc,以及
MVC
中的控制器,这里是用@controller`注释的组件


还请记住,如果控制器返回
字符串
,它必须是一个
重定向:
转发:
,或者(我猜在您的情况下,`要呈现的模板名称。

尝试用此代码替换您的代码,它会解决问题。
它是如何工作的。
当你用类似这样的URL点击浏览器时,你会触发这个控制器,现在这个控制器会加载你的jsp页面,
helloworld
,它被
viewHolder
调用,现在我们创建ModelAndView对象,并根据我们的要求传递值,
viewHolder
将包含他计算值并将其传递回控制器,然后控制器将其作为响应传递回用户

 @Controller
    public class HelloController {

      // Should I add a "produces" attribute to this? I don't know what to use for jsp.
      @RequestMapping("/hello")
      public getForm() {
        ModelAndView model= new ModelAndView("helloworld");
        model.addObject("greeting", "Hello Spring MVC");
        return model;
      }
    }  
也可以将此函数作为字符串类型编写

@Controller
    public class HelloController {
@RequestMapping("/hello")
      public string getForm(ModelMap model) {
        model.addAttributes("greeting", "Hello Spring MVC");
        return "helloworld";
      }
    }      
如果它仍然不工作,请检查spring dispatcher servlet和web xml是否定义正确。
在SpringDispatcherServlet中应该有这样的东西

<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>

    </bean>

/WEB-INF/views/
.jsp

WEB-INF是我在src中的根文件夹,views是包含jsp页面的文件夹。问题是多方面的,但最重要的细节是我如何打包应用程序以及如何启动它将文件打包为.war文件,声称.jar文件不起作用。事实证明,.jar文件偶尔起作用,但.war文件始终起作用。但应用程序需要从该文件启动,而不是从IDE构建的磁盘上的类文件启动。一旦我开始从打包的war文件启动它,它就开始工作了k


也就是说,关于如何编写映射方法,有各种各样的建议,它们似乎都很好,所以这不是问题。

你能解释一下你的答案吗?将
@RestController
更改为
@Controller
会让我无法将请求映射到我的方法。(我尝试了。)即使我将控制器与Rest接口分开(这听起来是个好主意),我仍然需要将输入映射到一个方法,并返回一个将被解释为.jsp文件名的值。您的语句很有意义,但代码示例会有所帮助。@MiguelMunoz将@RestController更改为@Controller使我无法映射请求我的方法。这是为什么?
@RequestMappings
在这两种情况下都能工作。问得好。当我第一次尝试它时,它不起作用。现在它似乎能工作,尽管我没有看到我的jsp页面。但是,我现在收到一条错误消息,提示它正在查找具有正确名称的jsp文件。我仍有一些问题需要解决,但看起来我正在取得进展。您的建议使我更接近目标,但仍然没有达到目标。现在我收到一条错误消息,上面说:“出现意外错误(type=Not Found,status=404)。/templates/pages/helloworld.jsp”所以它在正确的路径上搜索.jsp文件,但没有找到它,该文件具有正确的路径,但仍然无法工作。据我所知,它一定与Spring Boot配置的方式有关,但我不知道如何修复它。您的代码未编译。您可以说:[public ModelAndView hello(“helloworld”){]您可以在应该声明参数的位置传递一个值。也就是说,我尝试了类似的操作。我声明了一个Model类型的参数,并将其传递给ModelAndView的构造函数。然后我尝试添加属性名称/值,返回ModelAndView。我在Model和ModelAndView上都尝试了此操作。这两种情况下,br都不起作用owser给了我这个消息:出现了一个意外错误(type=notfound,status=404)./templates/pages/helloworld.jsp。但是你可能是对的。我已经用适当的解释更新了答案,希望这能帮助你使用Maven来构建?因为Maven似乎不希望在src文件夹中找到WEB-INF。当我把它放在那里时,在我构建之后,它不会显示在jar文件中。另外,你使用的是Spring Boot吗?Th这里的任务不是如何让jsp文件工作。而是让它们在Spring Boot中工作。我认为Spring Boot不应该需要web.xml文件,如果需要,我不确定应该放在哪里。是的,我正在使用MAVEN配置我的项目和构建。谢谢。所以我接下来的两个问题是:1)如何让它接受jar文件中的WEB-INF文件夹。2)您使用的是SpringBoot吗?
@Controller
    public class HelloController {
@RequestMapping("/hello")
      public string getForm(ModelMap model) {
        model.addAttributes("greeting", "Hello Spring MVC");
        return "helloworld";
      }
    }      
<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>

    </bean>