这里我不想编写 @requestMapping ,除了将在 spring.xml 文件中写入的内容。 我想知道这两种情况,比如我是不是在使用@requestmapping类级别和方法级别,我必须写什么?
答案 0 :(得分:2)
要配置SpringMVC,有两种方法 XML配置和注释配置:
在 spring-mvc-config.xml 中:我们将/hello
映射到helloWorldController
<beans ...>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello">helloWorldController</prop>
</props>
</property>
</bean>
<bean id="helloWorldController" class="xx.yy.zz.HelloWorldController" />
</beans>
HelloWorldController
应该从AbstractController
延伸并实施handleRequestInternal()
:
public class HelloWorldController扩展了AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView("hello");
model.addObject("message", "HelloWorld!");
return model; //will go to hello.jsp
}
}
@Controller
public class HelloWorldController
{
@RequestMapping("/hello")
protected ModelAndView hello() throws Exception {
ModelAndView model = new ModelAndView("hello");
model.addObject("message", "HelloWorld!");
return model; //will go to hello.jsp
}
}