Build SPRINGMVC Frame
1. Add a JAR Package
Jsp-api.jar
Servlet-api.jar
Jstl.jar
Commons-logging-1.1.1.jar
Spring-beans-4.1.6.release.jar
Spring-context-4.1.6.release.jar
Spring-core-4.1.6.release.jar
Spring-expression-4.1.6.release.jar
Spring-tx-4.1.6.release.jar
Spring-web-4.1.6.release.jar
Spring-webmvc-4.1.6.release.jar
2. Defining a controller and creating a JSP page
controller needs to implement controller interface
/*** Custom controller requires implementation of controller interface **/ public classHellocontrollerImplementsController{@Override publicmodelandview handlerequest (httpservletrequest req, httpservletresponse resp)throwsException {//calling a class to handleSystem.out.println ("-----hello controller-------"); //creates a Modelandview object that is finally rendered as the specified view by the view rendererModelandview mv =NewModelandview (); //sets the view name----finally resolves to the specified pageMv.setviewname ("hello"); //Add result data This data can be obtained through the El expression in the pageMv.addobject ("msg", "first Spring MVC app"); returnmv; }}
3. Configuration
You need to configure the Handlermapping mapper and the Handleradapter adapter and conteroller, as well as configure the view renderer
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "http://www.springframework.org/schema/beans"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans. XSD "> <!--Configuring the Handlermapper mapper - <Beanclass= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--Configure controller Custom controllers - <Beanname= "/hello.do"class= "cn.sxt.controller.HelloController"/> <!--Configuring the Handeradapter adapter - <Beanclass= "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--Configure the View renderer - <BeanID= "jspviewresolver"class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> < propertyname= "viewclass"value= "org.springframework.web.servlet.view.JstlView"/> <!--prefix the view name after rendering the view - < propertyname= "prefix"value= "/web-inf/jsp/"/> <!--suffix of post-rendered view - < propertyname= "suffix"value= ". jsp"/> <!--example: The view is named: hello after rendering:/web-inf/jsp/hello.jsp the page - </Bean></Beans>
4. Code Structure
5. Configure the Project Access path
6. Launch Tomcat Access http://localhost:8080/hello/hello.do
First Spring MVC App
SPRINGMVC Framework Study Notes (1)--helloworld