1. Create a Dynamic Web project
2. Modify the Web-inf/web.xml as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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/applicationcontext.xml</Param-value> </Init-param> </servlet> <servlet-mapping> <Servlet-name>Springmvc</Servlet-name> <Url-pattern>/</Url-pattern> </servlet-mapping></Web-app>
3. Create/web-inf/applicationcontext.xml and add the following:
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/be Ans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframewo Rk.org/schema/context/spring-context-3.0.xsd "> <Context:component-scanBase-package= "Com.vito.action" /> <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"> <!--This configuration is the location of the JSP page, which is configured according to your - <value>/web-inf/view/</value> </ Property> < Propertyname= "suffix"> <value>. jsp</value> </ Property> </Bean> </Beans>
4. Create a Java file, note the package name
Packagecom.vito.action; ImportOrg.springframework.stereotype.Controller; ImportOrg.springframework.ui.ModelMap; Importorg.springframework.web.bind.annotation.RequestMapping; ImportOrg.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping ("/welcome") Public classHelloworldcontroller {@RequestMapping (value= "/hello", method =requestmethod.get) PublicString Printwelcome (Modelmap model) {Model.addattribute ("Message", "Spring 3 MVC Hello World"); return"Hello"; } }
5. "Key" Create Web-inf/lib, import the following jar package, take care not to fill in the extra package. Because all the sping jar packages were imported before, resulting in an error, it took a day to find the cause.
6. Add the above jar to the build path
7. Try Http://localhost:8080/DemoSpringMVC/welcome/hello!
Spring MVC Environment Setup and configuration