简体   繁体   中英

Could not start Spring MVC project

I create a dynamic web project to develop a simple project, the first step I want to do is display the index page and show the title generate by controller. but it's always 404 status and no error or exception in log is there any problem?

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>

dispatcher-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>

You have not implemented the interfaces for your controller as you added @Controller annotation, But you have failed to add its suppert in the Dispatcher-servlet

try adding ,

<mvc:annotation-driven />

in your dispatcher-servlet. Also it is enough to give the url pattern in the controller without extension as below

@Controller
public class APIController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String welcome(ModelMap model) {

    model.addAttribute("title", "AM Core Test");
    return "index";
    }
}

Now try the URL as , http:\\\\ project_name\\index.web in your browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM