gpt4 book ai didi

java - Spring MVC 无法解析 .jsp 文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:42 26 4
gpt4 key购买 nike

我正在尝试构建 RESTful Web 服务。我按照 spring.io 的说明进行操作,并使“hello 项目”(localhost:8080/greeting) 正常工作。PS:我使用的是Maven项目。该项目已经发布在Github上:https://github.com/Tommzy/OutlierDemo/

但是,当我试图让 spring mvc 解析我的 View 之一(.jsp)时。据说

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Mar 25 01:31:04 EDT 2015 There was an unexpected error (type=Not Found, status=404). No message available

我不确定为什么总是发生这种情况,因为我已经有一个如下的配置文件。

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="config")
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

}

我的项目的整个目录树是:

src-main|- java   |- config  |-  WebConfig.java

|- hello |- Application.java
|- Greeting.java
|- GreetingController.java
|- PMethodController.java

|- webapp |- WEB-INF |- views |- pmethod.jsp
|- web.xml

我从 friend 那里复制了 web.xml。该文件在他的项目中运行良好。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>mvc-Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>config</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>mvc-Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
</web-app>

和我的 PMethodController.java:

@Controller
public class PMethodController {

@RequestMapping("/method1")
public String handleRequest() {
return "pmethod";
}

}

我的应用程序.java

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

取决于以上所有内容,如果我运行该项目然后输入 http://localhost:8080/method1它将在页面顶部显示白标错误页面。

最佳答案

试试这个:

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}

关于java - Spring MVC 无法解析 .jsp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29248615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com