Reputation: 153
Here I don't want to write @requestMapping so except that what will write in spring.xml file. I want to know both scenarios like if I am not using @requestmapping both class level and method level what I have to write ?
Upvotes: 0
Views: 501
Reputation: 3749
To configure your SpringMVC there are two ways XML Config and Annotation Config :
In spring-mvc-config.xml : here we mapped /hello
to 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
should extends from AbstractController
and implement handleRequestInternal()
:
public class HelloWorldController extends 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
}
}
Upvotes: 2