我尝试使用Spring Tools Suite设置一个简单的弹簧网,但我无法触发Controller类,我不确定是什么错,当我走http://localhost:8080/时,没关系,意味着我的服务器已启动,但当我转到http://localhost:8080/MVCTest/test12时,它显示找不到页面。
我的pom.xml没问题,我只有那里的所有库,我试图使用类符号,而不是XML。
设置
服务器:Pivotal tc Server Developer Edition v3.2 [已启动]
从控制台登录,似乎没问题:
Jan 14, 2017 12:29:34 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1108 ms
Jan 14, 2017 12:29:35 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 875 ms
我的控制器代码:
package com.app1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class EmployeeController {
@RequestMapping("/test12")
public void test12()
{
System.out.println("Welcome");
}
}
的web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>MVCTest</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servlet的context.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.app1.controller" />
答案 0 :(得分:0)
您没有必要的Spring Application Context定义文件。此文件确保Spring容器执行组件扫描,以便自动发现使用@Controller注释的任何代码。
浏览this链接了解详情。