一、项目的创建

1、创建webapp项目

2、pom文件的配置

  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId><!--全套关于Servlet的依赖-->
  4. <version>3.1.0</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-webmvc</artifactId><!--添加SpringMVC的依赖-->
  10. <version>5.2.5.RELEASE</version>
  11. </dependency

3、相关注解

  1. //创建一个普通类
  2. @Controller //控制器注释
  3. @RequestMapping("/common")//模块名称(类公共地址)
  4. class MyController{
  5. //value={"/doSom.do","",""},自定义接收的请求地址类型
  6. //method = RequestMethod.POST,POST,GET等为枚举类型,这里是自定义接收请求的方式
  7. @RequestMapping(value={"/doSom.do","",""},method = RequestMethod.POST)
  8. public ModelAndView doSome(){
  9. ModeAndView mv = new ModelAndView();
  10. //用请求作用域对象添加KEY和VALUE
  11. mv.addObject("key","StringValues");
  12. ......
  13. //请求转发
  14. mv.setViewName("show");
  15. }
  16. }

4、web.xml文件配置

  1. Servvlet配置
  2. <servlet>
  3. <servlet-name>disPatcherServlet(自定义或不自定义类名)</servlet-name>
  4. <servlet-class>org.springframework.web.servlet.DisPatcherServlet</servlet-class>
  5. <init-param>
  6. <param-name>contextConfigLocaltion</param-name>
  7. <param-value>classpath:applicationContext.xml</param-value>
  8. </init-param>
  9. <load-on-start>1</load-on-start>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>disPatcherServlet</servlet-name>
  13. <url-pattern>*.do</url-pattern>
  14. </servlet-mapping>
  15. 字符过滤器设置
  16. <filter>
  17. <filter-name>CharacterEncodingFilter</filter-name>
  18. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  19. <!--
  20. 框架源码参数
  21. private String encoding;
  22. private boolean forceRequestEncoding;
  23. private boolean forceResponseEncoding;
  24. -->
  25. 指定字符集
  26. <init-param>
  27. <init-name>encoding</init-name>
  28. <param-value>utf-8</param-value>
  29. </init-param>
  30. 强制设置请求编码
  31. <init-param>
  32. <param-name>forceRequestEncoding</param-name>
  33. <param-value>true</param-value>
  34. </init-param>
  35. 强行设置响应编码
  36. <init-param>
  37. <param-name>forceResponseEncoding</param-name>
  38. <param-value>true</param-value>
  39. </init-param>
  40. </filter>
  41. <filter-mapping>
  42. <filter-name>characterEncodingFilter</filter-name>
  43. <url-pattern>/*</url-pattern>
  44. </filter-mapping>

5、applicationContext.xml配置

  1. 组件扫描器
  2. <context:component-scanner base-package="com.bjpowernode.controller"/>
  3. 配置转发地址( mv.setViewName("show");)
  4. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  5. <property name="prefix" value="/WEB-INF/view/"/>
  6. <property name="suffix" value=".jsp"/>
  7. </bean>

6、jsp文件

  1. index.jsp
  2. <a href="doSome.do">点击这个</a>
  3. <a href="other.do">这是Other</a>
  4. ......
  5. show.jsp
  6. <center>
  7. <h4>${msg}</h4>
  8. <h4>${msgShow}</h4>
  9. </center>
  10. showOther.jsp
  11. <center>
  12. <h1>this is other</h1>
  13. <h4>${msg}</h4>
  14. <h4>${msgShow}</h4>
  15. </center>

7、目录结构

image.png