Java-springMVC框架:springMVC简单搭建一

小咪咪 2022-07-13 12:10 303阅读 0赞

一个简单的MVC框架,基本要熟悉以下步骤

  1. 1:定义一个Controller
  2. 2:配置如何配置
  3. 3:如何定义url地址 进入方法
  4. 4:如何将查询的数据放入作用域(拿到request,response对象)
  5. 5:如何返回页面,转发/重定向

springmvc的配置:

1: 引入jar包

  1. commons-beanutils-1.8.0.jar
  2. commons-fileupload-1.2.1.jar
  3. commons-io-2.0.1.jar
  4. commons-lang3-3.1.jar
  5. commons-logging-1.1.3.jar
  6. commons-pool-1.2.jar
  7. spring-aop-4.1.5.RELEASE.jar
  8. spring-aspects-4.1.5.RELEASE.jar
  9. spring-beans-4.1.5.RELEASE.jar
  10. spring-context-4.1.5.RELEASE.jar
  11. spring-context-support-4.1.5.RELEASE.jar
  12. spring-core-4.1.5.RELEASE.jar
  13. spring-expression-4.1.5.RELEASE.jar
  14. spring-web-4.1.5.RELEASE.jar
  15. spring-webmvc-4.1.5.RELEASE.jar
  16. spring-webmvc-portlet-4.1.5.RELEASE.jar

2:在web.xml定义Springmvc的核心类Servlet类

  1. springmvc的配置和applicationContext.xml是一种父子关系
  2. web-inf下建立一个叫:"servlet-name"-servlet.xml文件 zpupload-servlet.xml
  3. 如果想改变名称:在servlet核心类中加入:
  4. <init-param>
  5. <param-name>namespace</param-name>
  6. <param-value>fylupload</param-value>
  7. </init-param>
  8. web.xml中所有代码如下:
  9. <display-name>zpupload</display-name>
  10. <welcome-file-list>
  11. <welcome-file>index.jsp</welcome-file>
  12. </welcome-file-list>
  13. <!-- 加载和初始化spring应用上下文 application.setAttribute("contextConfigLocation","classpath:applicationContext.xml") -->
  14. <context-param>
  15. <param-name>contextConfigLocation</param-name>
  16. <param-value>classpath:applicationContext.xml</param-value>
  17. </context-param>
  18. <!-- 这个监听器就是专门去坚挺上面application的作用域的变化,如果发生了改变(setAttribute),那么它就会把spring 应用用上下文applicationContext(WebApplicationContext)初始化放入到appliation的作用域 -->
  19. <listener>
  20. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  21. </listener>
  22. <!-- springmvc的核心类 -->
  23. <servlet>
  24. <servlet-name>zpupload</servlet-name>
  25. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  26. <init-param>
  27. <param-name>namespace</param-name>
  28. <param-value>fylupload</param-value>
  29. </init-param>
  30. </servlet>
  31. <servlet-mapping>
  32. <servlet-name>zpupload</servlet-name>
  33. <url-pattern>*.html</url-pattern>
  34. </servlet-mapping>
  35. <!-- 乱码问题,post请求乱码 -->
  36. <filter>
  37. <filter-name>encoding</filter-name>
  38. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  39. <init-param>
  40. <param-name>encoding</param-name>
  41. <param-value>UTF-8</param-value>
  42. </init-param>
  43. </filter>
  44. <filter-mapping>
  45. <filter-name>encoding</filter-name>
  46. <url-pattern>/*</url-pattern>
  47. </filter-mapping>

3:配置fylupload.xml

  1. <!-- 扫包 -->
  2. <context:component-scan base-package="com.zp.upload"></context:component-scan>
  3. <!-- 视图渲染 -->
  4. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  5. <!-- 制定页面存放的路径 -->
  6. <property name="prefix" value="/WEB-INF/pages/"></property>
  7. <!-- 文件的后缀 -->
  8. <property name="suffix" value=".jsp"></property>
  9. </bean>

4:定义一个类IndexController测试

  1. package com.zp.upload;
  2. import java.util.HashMap;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.servlet.ModelAndView;
  9. @Controller
  10. @RequestMapping("/upload")
  11. public class IndexController {
  12. @RequestMapping("/test")
  13. public String test() {
  14. System.out.println("进来了嘛??");
  15. return null;
  16. }
  17. // http://localhost:8080/zpupload/upload/index1.html
  18. @RequestMapping("/index1")
  19. public String index1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
  20. HashMap<String, Object> map = new HashMap<>();
  21. map.put("name", "MM-index1");
  22. map.put("age", "17");
  23. // 把map放入作用域,jsp用EL表达式取值
  24. request.setAttribute("map", map);
  25. // 返回page,路径为视图渲染配种中的
  26. // name="prefix"+@RequestMapping("/upload")+@RequestMapping("/index1")+name="suffix"
  27. // 当我们访问 http://localhost:8080/zpupload/upload/index1.html 他实际找的jsp文件为
  28. // /WEB-INF/pages/upload/index1.jsp
  29. return "index1";
  30. }
  31. @RequestMapping("index2")
  32. public ModelAndView index2() {
  33. ModelAndView modelAndView = new ModelAndView();
  34. HashMap<String, Object> map = new HashMap<>();
  35. map.put("name", "MM-index2-ModelAndView");
  36. map.put("age", "18");
  37. modelAndView.addObject("map", map);// request.setAttribute("map", map);
  38. modelAndView.setViewName("upload/index2");// return "index1";
  39. return modelAndView;
  40. }
  41. // 带参数的get访问
  42. // http://localhost:8080/zpupload/upload/index3.html?username=你好
  43. @RequestMapping("index3")
  44. public String index3(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
  45. String username = request.getParameter("username");
  46. System.out.println("username========>" + username);
  47. HashMap<String, Object> map = new HashMap<>();
  48. map.put("name", "MM-index3-Parameter");
  49. map.put("age", "18");
  50. request.setAttribute("map", map);
  51. return "index3";
  52. }
  53. // 重定向:http://localhost:8080/zpupload/upload/index4.html?username=你好
  54. @RequestMapping("index4")
  55. public String index4(HttpServletRequest request) {
  56. String username = request.getParameter("username");
  57. System.out.println("username========>" + username);
  58. HashMap<String, Object> map = new HashMap<>();
  59. map.put("name", "MM-index4-redirect");
  60. map.put("age", "18");
  61. request.setAttribute("map", map);
  62. // return "redirect:success.html?username=mm";
  63. // return "redirect:/success.jsp";
  64. return "forward:/success.jsp";
  65. /**
  66. * return "redirect:/success.jsp";时
  67. * 会跳转 http://localhost:8080/zpupload/success.jsp
  68. * 访问WebContent下的success.jsp文件
  69. * return "redirect:success.jsp"; ---> 不加/
  70. * 是基于命名空间@RequestMapping("/upload"),会跳转/zpupload/upload/success.jsp
  71. * 重定向(不走视图解析,不会拼接路径)一定是webroot下面的页面或者是requestMapping路径,
  72. * WEB-INF下的文件不允许直接访问,需要借助中间跳转
  73. * return "redirect:/success.html"; 时
  74. * 会跳转 http://localhost:8080/zpupload/upload/success.html
  75. * 被@RequestMapping("/success")拦截 访问return "common/success";
  76. * 访问 /WEB-INF/pages/common/success.jsp 文件
  77. *
  78. * return "forward:/success.jsp";
  79. * 转发也会跳过视图解析,不拼接路径,访问WebContent下的success.jsp文件
  80. */
  81. }
  82. @RequestMapping("/success")
  83. public String success() {
  84. return "common/success";
  85. }
  86. }

代码详见

  1. http://download.csdn.net/detail/u013820054/9732369

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringMVC框架

    框架项目请跳转到: [springMVC][] 本文在他人框架配置文件的基础上,学习Spring框架的知识,同时也结合多篇博文对框架进行改进与增加工鞥,比如缓存、多数据源以及