我正在使用Spring 3.2版开发我的第一个应用程序。
似乎我已经检查了所有内容,但很多次,但我在尝试获取localhost时只收到404:8080 / MakeAwayTickets /或localhost:8080 / MakeAwayTickets / index。控制器甚至没有被加载,因为我看不到它的构造函数的任何输出。
正在加载css和js目录下的文件,例如localhost:8080 / MakeAwayTickets / css / dashboard.css。
日志包含:
2014-05-06T15:56:33.031+0400|INFO: visiting unvisited references
2014-05-06T15:56:33.446+0400|INFO: WebModule[null] ServletContext.log():No Spring WebApplicationInitializer types detected on classpath
2014-05-06T15:56:33.783+0400|SEVERE: log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
2014-05-06T15:56:33.783+0400|SEVERE: log4j:WARN Please initialize the log4j system properly.
2014-05-06T15:56:34.013+0400|INFO: WebModule[null] ServletContext.log():Initializing Spring FrameworkServlet 'appServlet'
2014-05-06T15:56:35.017+0400|INFO: Loading application [MakeAwayTickets] at [/MakeAwayTickets]
2014-05-06T15:56:35.081+0400|INFO: MakeAwayTickets was successfully deployed in 6,888 milliseconds.
我在配置中有以下内容:
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>MakeAwayTickets</display-name>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/root-context.xml
</param-value>
</context-param>
<filter>
<filter-name>charsetFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
servlet的context.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<annotation-driven />
<resources mapping="/css/**" location="/css/" />
<resources mapping="/bootstrap/**" location="/bootstrap-3.1.1-dist/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/pages/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:annotation-config />
<context:component-scan base-package="org.badcoding.spring.controller" />
</beans:beans>
这是控制器类:
package org.badcoding.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.badcoding.dao.interfaces.TicketsDAO;
import org.badcoding.spring.form.IndexSearch;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class IndexController {
public IndexController() {
System.out.println("here");
}
// @Autowired
// private TicketsDAO ticketsDAO;
@RequestMapping("/index")
public String index() {
return "index";
}
@RequestMapping("/")
public String root() {
return "redirect:/index";
}
@RequestMapping(value = "/index_search", method = RequestMethod.GET, headers="myHeader=myValue")
public String list_flights(@PathVariable String city_out, @PathVariable String city_in, @PathVariable String company, @PathVariable String date0, @PathVariable String date1, Model model) {
model.addAttribute("integer", 666);
return "index_search";
}
}
我真的需要帮助。
更新: 添加后
<beans:bean class="org.badcoding.spring.controller.IndexController"/>
到servlet配置我收到ClassNotFoundException。
答案 0 :(得分:0)
1.尝试从配置文件中删除以下代码
<context:annotation-config />
2.并在此<annotation-driven />
<mvc:annotation-driven />
3.尝试在控制器类中添加此代码
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
答案 1 :(得分:0)
我遇到了同样的问题,发现了这个问题。
虽然我是以编程方式配置的,但是找不到控制器的问题是因为我有@ComponentScan(“com.ables.books.model”)。
当然控制器不在那个包中,所以永远不会找到它们。
我已将其更改为@ComponentScan(“com.ables.books”),因此可以在任何地方找到携带@Controller的我的类。 xml中@ComponentScan的等价物是context:annotation-config,所以只需检查传递给它的内容。