Getting to know springmvc for the first time

1. Create a maven web project
2. Add servletAPI coordinates in pom.xml, and modify the version of junit to 4.11, because version 3.8 of junit does not support the annotation form.
3. Import the maven jar package and add the following coordinates in pom.xml
<properties>
  <commons-lang.version>2.6</commons-lang.version>
  <slf4j.version>1.7.6</slf4j.version>
  <spring .version>4.1.3.RELEASE</spring.version>
  </properties>
  <dependencyManagement>
  <dependencies>
  <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-framework-bom</artifactId>
  < version>${spring.version}</version>
  <type>pom</type>
  <scope>import</scope>
  <


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
  </dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
</dependency>
  <dependency> 
    <groupId>commons-lang</groupId> 
    <artifactId>commons-lang</artifactId> 





















<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
</web-app>
b) 更新之后
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
5、 在web.xml的<web-app>节点中添加如下配置文件
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispathcerServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
  </init-param>
  </servlet>
  < servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

6. Create configs/spring in the WEB-INF directory under the webapp directory directory, create the mvc-dispatcher-servlet.xml file in this directory, the specific content is as follows.
<?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.

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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.xsd">

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="org.giles.springmvc.*">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsps/"/>
<property name="suffix" value=" .jsp"/>
</bean>
</beans>
7. Create a new java directory under the src/main directory in the project
8. Create a HelloController class in src/main/java
@Controller
public class HelloController {
@RequestMapping(value=" /hello")
public String hello(){
System.out.println("************************");
return "hello";
}
} 9. After writing and deploying to the Tomcat server, add the plugin node <plugins>     <
build> node in pom.xml plugin>           <groupId>org.apache.tomcat.maven</groupId>



          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
</plugins>
10、 运行项目Run As>Maven Build

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326464436&siteId=291194637