0

我知道有很多关于如何用tomcat和eclipse配置spring MVC的解释。只是想知道我错过了什么。我已经检查了其他解决方案,但没有一个可以帮助我解决这个问题。

这是我正在使用的文件:1)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" version="2.5">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

2) 调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.controle.pronto" />
</beans:beans>

3) index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>

4)HomeController.java包com.controle.pronto;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "home";
}
}

5)这是我的工作空间结构

src
    main
        webapp
            WEB-INF
                views
                    home.jsp
                dispatcher-servlet.xml
                web.xml
            index.jsp

当我尝试访问: - localhost:8080/controleponto - localhost:8080/controleponto/index - localhost:8080/controleponto/index.jsp 我进入 404 页面 - 请求的资源不可用。

如果您能就我做错的事情向我提供一些反馈,我将不胜感激。

谢谢,路易斯·阿马拉尔

4

3 回答 3

1

转到您的 server.xml 文件。你会在那里找到你的上下文根。像这样的东西

<Context docBase="yourApp" path="/yourApp" reloadable="true" source="org.eclipse.jst.jee.server:yourApp"/></Host>

localhost:8080/yourApp假设您在端口 8080 上运行 tomcat,请尝试加载。

于 2013-06-06T22:51:00.727 回答
0

首先,服务器会搜索索引文件。因此,将您的 index.jsp 文件传递​​到 webapp 目录之外。

另外,尝试添加您的 dispatcher-servlet.xml。

于 2014-01-03T04:25:48.007 回答
0

首先尝试使用 local:8080 ,如果它运行良好,则表示您的 Tomcat 运行良好。

不仅仅是将简单的 .html 文件放在 web-app 下并右键单击它作为服务器运行它,您可以在 eclipse 中看到它的运行,然后您就知道您的应用程序用于您的 web 应用程序的实际名称。

一切都会顺利进行,我自己已经面对这个问题很多时间了

于 2015-01-16T05:50:39.607 回答