有两种方法可以在春天@Controller
引入一种方法。即当签名中没有使用任何与弹簧相关的内容时。
即返回类型为操作的实际结果且参数不包含与流相关的人员的方法。除了春天的注释,只是纯粹的逻辑。这是一个简单的控制器,它总是路由到一个视图。
@RequestMapping(method = RequestMethod.GET, value = "/list/{day}")
@ModelAttribute("list")
public List<String> list(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day) {
System.out.println("list of somthing for the day of "+day);
return Arrays.asList("a,b,c,d".split(","));
}
注意,视图名称没有提到,但是以某种方式解决了(我是春天的新手并且不知道如何)。如果从浏览器访问方法,即http://localhost/test/list/2015-01-01
,则会导致错误404。错误是404&#34; /test/WEB-INF/jsp/list/2015-01-01.jsp"没找到。
我想要实现的目标:此特定方法将使用/test/WEB-INF/jsp/list.jsp作为视图。我想它应该注释,但我没有在文档中找到如何。我错过了什么?
答案 0 :(得分:0)
我想要实现的目标:这种特殊方法会使用 /test/WEB-INF/jsp/list.jsp作为视图
为此你必须像这样创建一个调度员:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<!-- the same thing is done with the AppConfig class with the annotation @ComponentScan(basePackages="" -->
<context:component-scan base-package="com.mypackage.*" />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
你的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<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>/</url-pattern>
</servlet-mapping>
</web-app>
在您的方法中(在控制器内部),您将返回视图名称:
@RequestMapping(method = RequestMethod.GET, value = "/list/{day}")
public ModelAndView list(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day) {
System.out.println("list of somthing for the day of "+day);
ModelAndView model=new ModelAndView();
model.setViewName("list");
model.addObject("list",Arrays.asList("a,b,c,d".split(",")))
return model;
}