我创建了一个动态Web项目来开发一个简单的项目, 我想要做的第一步是显示索引页面 并显示控制器生成的标题。 但它始终是404状态 并且日志中没有错误或异常 有什么问题吗?
APIController.java
@Controller
public class APIController {
@RequestMapping(value = "/index.web", method = RequestMethod.GET)
public String welcome(ModelMap model) {
model.addAttribute("title", "AM Core Test");
return "index";
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.web</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.web</welcome-file>
</welcome-file-list>
调度-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
的index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Spring MVC</title>
</head>
<body>
<h2>${ title }</h2>
<form method="POST">
<p>Request:</p>
<textarea name="name" id="request" rows="3" cols="50">
</textarea>
<p>
<input type="submit" value="Submit" />
</p>
</form>
<p>Response:</p>
<textarea id="response" rows="10" cols="50"></textarea>
答案 0 :(得分:0)
您添加@Controller
注释时未实现控制器的接口,但您未能在Dispatcher-servlet
尝试添加,
<mvc:annotation-driven />
在您的dispatcher-servlet中。也足以在控制器中给出url模式而没有扩展名,如下所示
@Controller
public class APIController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String welcome(ModelMap model) {
model.addAttribute("title", "AM Core Test");
return "index";
}
}
现在在浏览器中尝试使用 http:\\ project_name\index.web