Create a web Project
Suppose we have installed the dependent packages required by Spring, and some other extension packages, as well as the Jetty container and ps: Jetty container installation. See the previous article.
To run a web project, you must have a web. xml configuration file that is placed under the WebContent/WEB-INF/directory.
[Html]
<? 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" xmlns: web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<! -- Configuration file location; default:/WEB-INF/applicationContext. xml -->
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/applicationContext. xml </param-value>
</Context-param>
<! -- Context Spring listener -->
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>
<! -- Servlet control jump, here by default go back to the xml file of the spring-servlet.xml -->
<Servlet>
<Servlet-name> spring </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/spring-servlet.xml </param-value>
</Init-param>
</Servlet>
<! -- Url-pattern is the method used by Spring to listen for routes, and then searches for matching controllers.
For example:
<Url-pattern>/</url-pattern> is usually the/hello/say/URL method.
<Url-pattern> *. htm </url-pattern> is generally a URL such as/hello/say.htm.
-->
<Servlet-mapping>
<Servlet-name> spring </servlet-name>
<Url-pattern>/</url-pattern>
</Servlet-mapping>
</Web-app>
Running Spring requires the configuration file applicationContext. xml, and we place applicationContext. xml in the WEB-INF/directory.
[Html]
<? 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: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<Context: annotation-config/>
<Aop: aspectj-autoproxy/>
</Beans>
The spring-servlet.xml configures the specific Controller folder directory to be accessed by Spring and the template directory and the template suffix name.
[Html]
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: p = "http://www.springframework.org/schema/p"
Xmlns: mvc = "http://www.springframework.org/schema/mvc"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd>
<! -- Access the Controller file with @ Controller annotation under the com. mvc. rest package -->
<Context: component-scan base-package = "com. mvc. rest"/>
<! -- Map the URL to the Controller through annotation. The label registers DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter by default -->
<Mvc: annotation-driven/>
<! -- View configuration, directory to be configured, and suffix -->
<Bean id = "viewResolver"
Class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass"
Value = "org. springframework. web. servlet. view. JstlView"/>
<Property name = "prefix" value = "/WEB-INF/views/"/>
<Property name = "suffix" value = ". jsp"> </property>
</Bean>
</Beans>
We need to create a com. mvc. rest package and create a file named HelloController. java under the package. This file is the controller of MVC. Then, we have two methods: say And yes. The URLs are/hello/say/AND/hello/yes/
[Java]
Package com. mvc. rest;
Import org. springframework. stereotype. Controller;
Import org. springframework. web. bind. annotation. RequestMapping;
// @ Controller is an annotation label that identifies the Controller class. This annotation is required for Controller classes.
@ Controller
// @ RequestMapping (value = "/hello") will be mapped to url/hello/, then access the Action in HelloController
@ RequestMapping (value = "/hello ")
Public class HelloController {
// @ RequestMapping (value = "/say") is mapped to url/hello/say, and then the Action in HelloController is accessed.
@ RequestMapping (value = "/say ")
Public void say (){
System. out. print ("this is HelloController And say Action \ r \ n ");
}
@ RequestMapping (value = "/yes ")
Public void yes (){}
}
We need to create a directory of views under the/WEB-INF/directory, and then create a directory of/views/hello/, which is say. jsp and yes. jsp is a template file.
Then run the configuration and run the web program through the Jetty container.
After running, the console displays the following information: