我没有配置Spring的经验,而且我很难这样做。我正在尝试做的是使用注释成功地将URL映射到Controller。此外,我想访问HttpRequest和HttpResponse。这是因为我想使用Jackson直接在字节流中编写和解析json。现在我知道Spring有使用Jackson内置的JSON视图,但我想先得到一个不错的立足点,因为现在我似乎无法正确配置映射。
的web.xml
<display-name>Kerris 2</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-config.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
应用-confix.xml
<context:annotation-config />
<context:component-scan base-package="servlet" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
DayController
package servlet;
@Controller
@RequestMapping("/days/*")
public class DayController {
private DayDAO dayDao;
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody void test(HttpResponse response){
System.out.println("Days GET");
}
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody void test2(HttpRequest request, HttpResponse response){
System.out.println("Days POST");
}
public void setDaydao(DayDAO dayDao) {
this.dayDao = dayDao;
System.out.println("Days Dao assigned");
}
}
当我查看我的服务器日志时,我可以在其中看到以下行
INFO: Mapped URL path [/days/*] onto handler 'dayController'
INFO: Mapped URL path [/days/*.*] onto handler 'dayController'
INFO: Mapped URL path [/days/*/] onto handler 'dayController'
当我在 contextroot / 测试应用程序时,我看到了标准的Hello World!页。当我尝试 contextroot / days / 时,我得到了404.当我尝试 contextroot / days / test 时也是404.有人能指出我做错了吗?< / p>
答案 0 :(得分:0)
发现我做错了什么。作为n00b,我没有意识到我必须专门定义一个DispatcherServlet,它将请求交给控制器。
<强> Web.xml中强>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-config.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<强> API-servlet.xml中强>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="servlet" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>
当我尝试拨打{contextroot} / api / days /时,请求到达控制器。