我设置了一个非常基本的 spring mvc Web 应用程序,这是我的 Controller 类中的开头/部分
@Controller
public class MainController {
@Autowired
AssetDAO assetDAO;
@RequestMapping("/")
public String home() {
System.out.println("HERE!");
return "index";
}
和我的index.jsp 文件
<%@page language="java" contentType="text/html; charset=UTF-8" paegeEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "~//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org.TR.html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Welcome Page </title>
</head>
<body>
<div>
<h1> Asset Tracking Web App Test </h1>
<a href="EmployeeList">Get Employee Records</a>
</div>
</body>
</html>
但是当我访问localhost:8080/
时,我收到404
错误。但是消息 HERE
被打印
编辑:我在 src/main/resources 文件夹中添加了一个 application.properties 文件,该文件解决了问题...我现在的问题是,这与使用 InternalViewResolver 有什么不同?或者我对此的理解是错误的??
EDIT2:项目的文件结构如下
.
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── assetTracking
│ │ ├── App.java
│ │ ├── AssetDAO.java
│ │ ├── Employee.java
│ │ └── MainController.java
│ ├── resources
│ │ └── application.properties
│ └── webapp
│ └── WEB-INF
│ ├── jsp
│ │ ├── employeeList.jsp
│ │ └── index.jsp
│ └── views.xml
├── tableCreatiosn.sql
├── tableEntries
为了简洁起见,省略了所有依赖项目录。耶茨建议的文件并没有解决问题。但是用 application.properties 替换它们确实如此
application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
welcome.message: Hello Test
最佳答案
可能您还没有配置InternalViewResolver 。如果是,那么它会将 index.jsp
定位在某个不正确的位置。
以下是InternalViewResolver
的配置
XML:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
Java
@EnableWebMvc
@Configuration
@ComponentScan("<package_fully_qualified_name>")
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
关于java - Spring MVC Controller 已达到,但我仍然得到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50952362/