我正在研究spring mvc,想要测试一些非常简单的基于注释的控制器,它在主页上工作正常但是当我输入 / home / user 时,它给了我 HTTP状态404 - Not Found 错误,我没有找到任何解决方法。
的web.xml
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--####################################-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
调度-servlet.xml中
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<context:component-scan base-package="spring" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--<bean id="viewResolver"
</beans>
HomeController.java
package spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
//@RequestMapping(value = "/home")
public class HomeController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String showHomeMessage() {
return "home";
}
}
UserController.java
package spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/home/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String welcomeMessage(){
return "user";
}
}
redirect.jsp 只使用sendRedirect()
方法并重定向到 home.jsp 。
home.jsp 和 user.jsp 只打印一个简单的 hello 消息
我做错了什么?
此致
答案 0 :(得分:1)
您是否尝试过嵌套public class customer{
private List<Movie> currentlyRented;
private List<Movie> rentingHistory;
public Customer(){
}
}
?像这样......
public class Catalogue{
private List<Movie> moviesAvailable;
private List<Movie> moviesRented;
public Catalogue(){
moviesAvailable.add(new Movie("Matrix",1999,new Genre("SciFi"),3));
moviesAvailable.add(new Movie("Jurassic Park",1993, new Genre("SciFi"),4));
}
}
我不确定您是否需要在RequestMapping
上设置值(即@Controller
@RequestMapping(value = "/home")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String showHomeMessage() {
return "home";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String welcomeMessage(){
return "user";
}
}
)。
答案 1 :(得分:0)
在查看this小例子之后,我认为您的问题与您未使用@ResponseBody
注释指定@Controller
这一事实有关。然而,如果您使用@RequestController
注释,仅仅暗示字符串类型就足够了,因为它结合了@Controller
和@ResponseBody
的使用
@Controller
@RequestMapping("/home/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String welcomeMessage(){
return "user";
}
}
同一块的替代方案是......
@RestController
@RequestMapping("/home/user")
public class UserController {
@RequestMapping(method = RequestMethod.GET)
public String welcomeMessage(){
return "user";
}
}
当然这个小例子并没有展示MediaType
等等,但希望你能得到这个想法。