Spring MVC Hello World应用程序无法正常工作 [英] Spring MVC Hello World application not working

查看:172
本文介绍了Spring MVC Hello World应用程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC的新手,我正在尝试编写一个简单的Web应用程序来开始使用Spring。

I'm new to Spring MVC and I'm trying to write a simple web app to get started with Spring.

这就是我所拥有的:

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"> 
    <display-name>HatifimWeb</display-name> 
    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
        <servlet-name>main</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>main</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app>

main-servlet.xml

main-servlet.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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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="springapp.controller" /> 
    <bean id="viewResolver" 
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="viewClass" 
            value="org.springframework.web.servlet.view.JstlView" /> 
        <property name="prefix" value="/WEB-INF/jsps/" /> 
        <property name="suffix" value=".jsp" /> 
    </bean> 
</beans>

LoginController.java

LoginController.java

package springapp.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class LoginController { 

    @RequestMapping("/Login") 
    public ModelAndView handleRequest() throws Exception { 
        System.out.println("Login..."); 

        return new ModelAndView("Login", "message", "test"); 
    } 

} 

index.jsp

index.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    Hello. 
    <br /> 
    <a href="Login">login</a> 
</body> 
</html> 

Login.jsp

Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
<title>poopidipoo</title> 
</head> 
<body> 
    ${message} 
</body> 
</html>

index.jsp启动良好,但当我点击导航到登录页面时,我得到404 。
我正在使用Tomcat 7.而且我没有想法,看起来DispatcherServlet由于某种原因没有被调用。

The index.jsp starts well, but when I click to navigate to the login page, I get 404. I'm using Tomcat 7. And I'm out of ideas, it looks like the DispatcherServlet is not being invoked for some reason.

什么可以是问题吗?

提前致谢。

推荐答案

问题可能是你错过了页面中的上下文路径,请尝试这种方式:

The issue could be that you are missing the context path in your page, try this way:

<a href="${pageContext.request.contextPath}/Login">login</a> 

更新:

您还可以将
< mvc:annotation-driven /> 添加到 main-servlet.xml

这篇关于Spring MVC Hello World应用程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆