1. Increase Spring Support
To create a new Web project, add the following jar package:
Commons-logging-1.2.jar Spring-aop-4.2.5.release.jar Spring-aspects-4.2.5.release.jar Spring-beans-4.2.5.release.jar
Spring-context-4.2.5.release.jar Spring-context-support-4.2.5.release.jar Spring-core-4.2.5.release.jar Spring-expression-4.2.5.release.jar
Spring-instrument-4.2.5.release.jar Spring-instrument-tomcat-4.2.5.release.jar Spring-jdbc-4.2.5.RELEASE.jar Spring-jms-4.2.5.release.jar
Spring-messaging-4.2.5.release.jar Spring-orm-4.2.5.release.jar Spring-oxm-4.2.5.release.jar Spring-test-4.2.5.release.jar
Spring-tx-4.2.5.release.jar Spring-web-4.2.5.release.jar Spring-webmvc-4.2.5.release.jar
Spring-webmvc-portlet-4.2.5.release.jar Spring-websocket-4.2.5.release.jar
2. Configure the front-end controller Dispatcherservlet
Configure the Web. xml file (add Dispatcherservlet configuration)
<! --Define spring MVC's Front controller--
<servlet>
<servlet-name>springmvc</servlet-name>
<sevlet-class>org.springframework.web.servlet.DispacherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-stratup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3, configure the controller SPRINGMVC
Configure the configuration file for Springmvc Spring-config.xml
<beans>
<!--configure handle, map "Hello" request--
<beanname= "/hello" class= "Org.fkit.conttroler.HelloController"/>
<!--processing Mapper looks up the bean as a URL and needs to specify the name (that is, the URL) when configuring handle.
<bean class= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--Simplecontrollerhandleradapter is a processor adapter that implements the Handleradapter interface for all processor adapters--
<bean class= "Org.springframework.web.servler.mvc.SimpleControllerHandlerAdapter"/>
<!--configuration View Resolver--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
4, the Controller class implementation
The Controller class must implement the Controller interface to handle/hello requests. (or use @controller annotations directly)
public class Hellocontroller implements controller{
Private static final Log logger = Logfactory.getlog (HELLOCONTROLLER.CALSS);
@Override
Public Modelandview HandleRequest (httpservletrequest request,httpservletresponse response) throws exception{
Logger.info ("HandleRequest is called");
Modelandview mv = new Modelandview ();
Mv.addobject ("message", "Hello world!");
Mv.setviewname ("/web-inf/content/welcome.jsp");
return MV;
}
}
5. View Page
<body>
<!--page to access the message--> passed by the controller
${requestscope.message}
</body>
6. Test Application
Http://localhost:8080/SpringmVCTest/hello
SPRINGMVC application Configuration