【发布时间】:2015-12-22 20:12:46
【问题描述】:
我想按照教程一步一步地创建一个简单的 hello world,但我认为缺少一些东西,因为我的代码只返回 404
请问您能帮我解决问题吗?
这是控制器类
package com.companyname.springapp;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by cota on 22/12/15.
*/
@Controller
public class HelloController{
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping(value="/hello.htm",method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("views/hello.jsp", "now", now);
}
}
这是 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"
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">
<display-name>Springapp</display-name>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
这是 app-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven/>
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.companyname.springapp.web" />
</beans>
这是一个包含所有JSP文件的头文件
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
并且 index.jsp 和 hello.jsp 有下一行
<%@ include file="views/include.jsp" %>
这是我在这里的第一个问题,真的很佩服你们所有人,对不起我的英语不好!
【问题讨论】:
-
...您的视图解析器配置是什么?我认为您还有其他问题,但最好一步一步来。
-
你打什么端点?当你的服务器启动时,你会看到什么输出?
-
问题很可能是因为 Spring 找不到文件
views/hello.jsp。你是怎么配置ViewResolver的? -
你可以发布你的网址吗?
标签: spring-mvc servlets