[关闭]
@ruoli 2016-08-26T17:10:50.000000Z 字数 4980 阅读 1584

简易SpringMvc框架搭建

SpringMvc


1、新建Maven工程,设置pom.xml

工程命名为:SpringMvc,设置pom.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>SpringMvc</groupId>
  5. <artifactId>SpringMvc</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <name>SpringMvcSimple Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-webmvc</artifactId>
  14. <version>4.0.2.RELEASE</version>
  15. </dependency>
  16. </dependencies>
  17. </project>

2、设置WEB-INF/web.xml

web.xml内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. version="3.0">
  7. <display-name>Archetype Created Web Application</display-name>
  8. <!-- spring MVC的核心就是DispatcherServlet,使用springMVC的第一步就是将下面的servlet放入web.xml
  9. servlet-name属性非常重要,默认情况下,DispatchServlet会加载这个名字-servlet.xml的文件,如下,就会加载
  10. dispather-servlet.xml,也是在WEN-INF目录下。
  11. -->
  12. <servlet>
  13. <servlet-name>dispatcher</servlet-name>
  14. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 -->
  18. <servlet-mapping>
  19. <servlet-name>dispatcher</servlet-name>
  20. <url-pattern>/</url-pattern>
  21. </servlet-mapping>
  22. <!-- 字符集过滤器 -->
  23. <filter>
  24. <filter-name>encodingFilter</filter-name>
  25. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  26. <init-param>
  27. <param-name>encoding</param-name>
  28. <param-value>UTF-8</param-value>
  29. </init-param>
  30. <init-param>
  31. <param-name>forceEncoding</param-name>
  32. <param-value>true</param-value>
  33. </init-param>
  34. </filter>
  35. <filter-mapping>
  36. <filter-name>encodingFilter</filter-name>
  37. <url-pattern>/*</url-pattern>
  38. </filter-mapping>
  39. </web-app>

3、新增dispatcher-servlet.xml文件

在WEB-INF下新增dispatcher-servlet.xml文件,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd"
  13. default-lazy-init="true">
  14. <!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
  15. <!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
  16. <mvc:resources location="/*.html" mapping="/**.html" />
  17. <mvc:resources location="/css/*" mapping="/css/**" />
  18. <mvc:resources location="/js/*" mapping="/js/**" />
  19. <mvc:resources location="/images/*" mapping="/images/**" />
  20. <!-- 添加注解驱动 -->
  21. <mvc:annotation-driven />
  22. <!-- 默认扫描的包路径 -->
  23. <context:component-scan base-package="controller" />
  24. <!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View -->
  25. <!-- 像下面这样设置,如果请求为/,则不通过controller,而直接解析为/index.jsp -->
  26. <mvc:view-controller path="/" view-name="index" />
  27. <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  28. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
  29. <!-- 配置jsp路径前缀 -->
  30. <property name="prefix" value="/"></property>
  31. <!-- 配置URl后缀 -->
  32. <property name="suffix" value=".jsp"></property>
  33. </bean>
  34. </beans>

4、修改index.jsp 文件

文件位置为:WebRoot/index.jsp,内容如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>简易登陆</title>
  8. </head>
  9. <body>
  10. <h2>Hello World!</h2>
  11. <form action="login">
  12. 用户名:<input id="username" name="username" type="text"></input><br>
  13. 密&nbsp;&nbsp;码:<input id="username" name="password" type="password"></input><br>
  14. <input type="submit">
  15. </form>
  16. <span>当前IP:<%=request.getRemoteAddr() %></span>
  17. </body>
  18. </html>

5、新增MyController.java 文件

文件目录为:controller/MyController.java,内容如下:

  1. package controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. /**
  7. * TODO 控制层代码
  8. * @author Ruoli
  9. * @date 2016年08月26日
  10. */
  11. @Controller
  12. public class MyController {
  13. @RequestMapping("login")
  14. public @ResponseBody String hello(
  15. @RequestParam(value = "username", required = false)String username,
  16. @RequestParam(value = "password", required = false)String password
  17. ){
  18. return "Hello "+username+",Your password is: "+password;
  19. }
  20. }

6、部署至Tomcat

启动访问 http://localhost:8080/SpringMvc/ 即可。
整个工程目录如下:
工程目录

7、附件下载

点击此处下载工程附件

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注