Spring MVC Configuration
1. Importing the JAR Package
2. Add configuration about SPRINGMVC in the Web. XML configuration file
<!--Configure the setting of Springmvcdispatcherservlet and configure the mapping-->
<!--spring is divided into multiple files for configuration, where the Init-param attribute is not specified in Servlet-name, and the spring configuration file that the system automatically looks for is [Servlet-name]-servlet.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: springmvc-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. Add springmvc-servlet.xml config file under src
<?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:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"><!--Scan the Packageand the Sub Package-<context:component-scan base- Package= "Test. Springmvc "/> <!--don T handle the static resource--<MVC:default-servlet-handler/> <!--ifYou use annotation must configure following setting-<mvc:annotation-driven/> <!--Configure the Internalresourceviewresolver-<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"ID= "Internalresourceviewresolver" > <!--prefix--<property name= "prefix" value= "/web-inf/jsp/"/> <!--suffix-<property name= "suffix" value= ". jsp"/> </bean></beans>
4. Create a folder named JSP under the Web-inf folder to hold the JSP view. Create a hello.jsp and add "Hello world" to the body.
5. Set up the package and controller
6. Writing Controller Code
@Controller @requestmapping ("/mvc") Public class Mvccontroller { @RequestMapping ("/hello") public String Hello () { return "Hello"; }}
7. Start the server
Java Framework-spring MVC application 001-spring MVC configuration