1. Web. xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns: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"> <Display-name>Springmvcdemo</Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list> <servlet> <Servlet-name>Springmvc</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/config/spring-mvc.xml</Param-value> </Init-param> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Springmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping></Web-app>
View Code
2. Spring-mvc.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/ spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring- Tx.xsd "> <Mvc:annotation-driven/> <Context:component-scanBase-package= "Com.jnl.springMvcDemo.controller" /> <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"> <value>/views/</value> </ Property> < Propertyname= "suffix"> <value>. jsp</value> </ Property> </Bean></Beans>
View Code
3. Navigationcontroller.java
PackageCom.jnl.springMvcDemo.controller; ImportOrg.springframework.stereotype.Controller; Importorg.springframework.web.bind.annotation.RequestMapping; ImportOrg.springframework.web.bind.annotation.RequestMethod; ImportOrg.springframework.web.servlet.ModelAndView; @Controller Public classNavigationcontroller {@RequestMapping (value= "/navigation", method=requestmethod.get) PublicModelandview getnavigations () {Modelandview mv=NewModelandview (); Mv.setviewname ("Result"); returnMV; } }
View Code
4. ADD a JSP file with name ' result ' in folder ' views '
5. Visit Http://localhost:8080/springMvcDemo/navigation
(the highlighted words mean value of requestmapping in Navigationcontroller.java)