1. Spring MVC
Like struts, it's an MVC framework, a bit similar to Strusts2, and spring seamlessly connected to a framework of spring.
2. Environment Construction
1) Create a new Web project in Eclipse
2) Add in Web. xml
<Servlet> <Servlet-name>Springmvc</servlet-name> <Servlet-class>Org.Springframework.Web.Servlet.Dispatcherservlet</servlet-class> <Init-param> <Param-name>Contextconfiglocation</param-name> <Param-value>Classpath*: config/spring-servlet.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>
3) Import the required jar packages
4) Edit the Web. xml file
<?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_2_ 5.xsd "id=" webapp_id " version=" 2.5 "> <display-name>Springmvc</display-name> <welcome-file-list> <welcome-file>Index.html</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>Classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></Web-app>
5) Edit the Spring-mvc.xml file
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http// Www.springframework.org/schema/context "xsi:schemalocation=" http://www.springframework.org /schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--packages that use annotations, including subsets-- <context:component-scan base-package="Com.model"/> <!--View resolver -- <bean id= "viewresolver"class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> < property name="prefix" value="/web-inf/jsp/" / > < property name="suffix" value=". jsp"></Property > </Bean></Beans>
6) Hellospring.java
package Com.model; import Org.springframework.stereotype.Controller; import Org.springframework.ui.Model; import org.springframework.web.bind.annotation.requestmapping; @Controller public class helloworldcontroller { @RequestMapping ("/helloworld ") public String helloworld (model model) {Model.addattribute ( " Message ", " Hello springmvc! "); return "HelloWorld" ; }}
Getting started with Spring MVC