SpringMVC入门

心已赠人 2021-09-19 05:30 549阅读 0赞

目录

示例1 :

示例2:

示例3:


watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70

新建dynamic web project

导入包:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 1

示例1 :

web.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  3. <display-name>ssm_spring_mvc</display-name>
  4. <servlet>
  5. <servlet-name>springmvc</servlet-name>
  6. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  7. <init-param>
  8. <!-- 读取springmvc配置文件 -->
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:springmvc.xml</param-value>
  11. </init-param>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>springmvc</servlet-name>
  16. <!-- 拦截规则 -->
  17. <!-- 1.*.htm,*.do,*.action以扩展名方式拦截,什么情况下都可以使用。但是不拦截静态资源如:.jpg,.css,.js,.png 。 -->
  18. <!-- 2./ 不拦截静态资源如:.jpg,.css,.js,.png -->
  19. <!-- /*全部拦截 包括jsp和所有静态资源。不推荐使用 -->
  20. <!-- <url-pattern>*.sikiedu</url-pattern> -->
  21. <url-pattern>/</url-pattern>
  22. </servlet-mapping>
  23. <welcome-file-list>
  24. <welcome-file>index.html</welcome-file>
  25. <welcome-file>index.htm</welcome-file>
  26. <welcome-file>index.jsp</welcome-file>
  27. <welcome-file>default.html</welcome-file>
  28. <welcome-file>default.htm</welcome-file>
  29. <welcome-file>default.jsp</welcome-file>
  30. </welcome-file-list>
  31. </web-app>

servlet-class标签的内容是DispatcherServlet的全包名 ,如下:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 2

新建“springmvc.xml”,并导入约束:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 3watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 4

springmvc.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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  11. <!-- 开启注解扫描 -->
  12. <context:component-scan base-package="com.sikiedu.controller"></context:component-scan>
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="prefix" value="/Views/"></property>
  15. <property name="suffix" value=".jsp"></property>
  16. </bean>
  17. </beans>

ItemController.java:

  1. /**
  2. *
  3. */
  4. package com.sikiedu.controller;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. /**
  9. *
  10. *
  11. * @author Lijiang
  12. *2019年5月3日
  13. */
  14. @Controller
  15. public class ItemController {
  16. @RequestMapping(value="welcome")
  17. public String Test(){
  18. return "helloworld";
  19. }
  20. }

helloworld.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>Insert title here</title>
  8. </head>
  9. <body>
  10. helloWorld
  11. </body>
  12. </html>

index.jsp:(注意:index.jsp放在webcontent目录下,不要放在web-inf文件夹下面!!!

  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>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="welcome">welcome to springmvc</a>
  11. </body>
  12. </html>

运行结果:

2019050313265769.gif

示例2:

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  3. <display-name>ssm_spring_mvc</display-name>
  4. <servlet>
  5. <servlet-name>springmvc</servlet-name>
  6. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  7. <init-param>
  8. <!-- 读取springmvc配置文件 -->
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:springmvc.xml</param-value>
  11. </init-param>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>springmvc</servlet-name>
  16. <!-- 拦截规则 -->
  17. <!-- 1.*.htm,*.do,*.action以扩展名方式拦截,什么情况下都可以使用。但是不拦截静态资源如:.jpg,.css,.js,.png 。 -->
  18. <!-- 2./ 不拦截静态资源如:.jpg,.css,.js,.png -->
  19. <!-- /*全部拦截 包括jsp和所有静态资源。不推荐使用 -->
  20. <!-- <url-pattern>*.sikiedu</url-pattern> -->
  21. <url-pattern>*.do</url-pattern>
  22. </servlet-mapping>
  23. <welcome-file-list>
  24. <welcome-file>index.html</welcome-file>
  25. <welcome-file>index.htm</welcome-file>
  26. <welcome-file>index.jsp</welcome-file>
  27. <welcome-file>default.html</welcome-file>
  28. <welcome-file>default.htm</welcome-file>
  29. <welcome-file>default.jsp</welcome-file>
  30. </welcome-file-list>
  31. </web-app>

springmvc.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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  11. <!-- 开启注解扫描 -->
  12. <context:component-scan base-package="com.sikiedu.controller"></context:component-scan>
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="prefix" value="/WEB-INF/jsp/"></property>
  15. <property name="suffix" value=".jsp"></property>
  16. </bean>
  17. </beans>

ItemController.java:

  1. /**
  2. *
  3. */
  4. package com.sikiedu.controller;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. /**
  9. *
  10. *
  11. * @author Lijiang
  12. *2019年5月3日
  13. */
  14. @Controller
  15. public class ItemController {
  16. @RequestMapping(value="list.do")
  17. public ModelAndView Test(){
  18. ModelAndView mav=new ModelAndView();
  19. mav.setViewName("item_list");
  20. return mav;
  21. }
  22. }

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>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="list.do">welcome to springmvc</a>
  11. </body>
  12. </html>

运行结果:

20190503135735704.gif

示例3:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 5

web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  3. <display-name>ssm_spring_mvc</display-name>
  4. <servlet>
  5. <servlet-name>springmvc</servlet-name>
  6. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  7. <init-param>
  8. <!-- 读取springmvc配置文件 -->
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:springmvc.xml</param-value>
  11. </init-param>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>springmvc</servlet-name>
  16. <!-- 拦截规则 -->
  17. <!-- 1.*.htm,*.do,*.action以扩展名方式拦截,什么情况下都可以使用。但是不拦截静态资源如:.jpg,.css,.js,.png 。 -->
  18. <!-- 2./ 不拦截静态资源如:.jpg,.css,.js,.png -->
  19. <!-- /*全部拦截 包括jsp和所有静态资源。不推荐使用 -->
  20. <!-- <url-pattern>*.sikiedu</url-pattern> -->
  21. <url-pattern>*.do</url-pattern>
  22. </servlet-mapping>
  23. <welcome-file-list>
  24. <welcome-file>index.html</welcome-file>
  25. <welcome-file>index.htm</welcome-file>
  26. <welcome-file>index.jsp</welcome-file>
  27. <welcome-file>default.html</welcome-file>
  28. <welcome-file>default.htm</welcome-file>
  29. <welcome-file>default.jsp</welcome-file>
  30. </welcome-file-list>
  31. </web-app>

springmvc.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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
  11. <!-- 开启注解扫描 -->
  12. <context:component-scan base-package="com.sikiedu.controller"></context:component-scan>
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="prefix" value="/WEB-INF/jsp/"></property>
  15. <property name="suffix" value=".jsp"></property>
  16. </bean>
  17. </beans>

ItemController.java:

  1. /**
  2. *
  3. */
  4. package com.sikiedu.controller;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.servlet.ModelAndView;
  12. import com.sikiedu.bean.ItemInfo;
  13. import jdk.nashorn.internal.ir.RuntimeNode.Request;
  14. /**
  15. *
  16. *
  17. * @author Lijiang
  18. *2019年5月3日
  19. */
  20. @Controller
  21. public class ItemController {
  22. @RequestMapping(value={"list.do","mylist.do"},method={RequestMethod.GET,RequestMethod.POST })
  23. public ModelAndView Test(){
  24. ModelAndView mav=new ModelAndView();
  25. ItemInfo info1=new ItemInfo("1", "王者荣耀", "moba", "0");
  26. ItemInfo info2=new ItemInfo("2", "王者荣耀", "moba", "0");
  27. ItemInfo info3=new ItemInfo("3", "王者荣耀", "moba", "0");
  28. ItemInfo info4=new ItemInfo("4", "王者荣耀", "moba", "0");
  29. List<ItemInfo> itemList=new ArrayList<ItemInfo>();
  30. itemList.add(info1);
  31. itemList.add(info2);
  32. itemList.add(info3);
  33. itemList.add(info4);
  34. mav.addObject("itemList", itemList);
  35. mav.setViewName("item_list");
  36. return mav;
  37. }
  38. // 转发
  39. @RequestMapping("forwardString.do")
  40. public String forwardString(){
  41. return "forward:list.do";
  42. }
  43. // 重定向
  44. @RequestMapping("redirectString.do")
  45. public String redirectString(){
  46. return "redirect:/index.jsp";
  47. }
  48. @RequestMapping(value="testList.do")
  49. public String Test1(Model model){
  50. ItemInfo info1=new ItemInfo("1", "王者荣耀", "moba", "0");
  51. ItemInfo info2=new ItemInfo("2", "王者荣耀", "moba", "0");
  52. ItemInfo info3=new ItemInfo("3", "王者荣耀", "moba", "0");
  53. ItemInfo info4=new ItemInfo("4", "王者荣耀", "moba", "0");
  54. List<ItemInfo> itemList=new ArrayList<ItemInfo>();
  55. itemList.add(info1);
  56. itemList.add(info2);
  57. itemList.add(info3);
  58. itemList.add(info4);
  59. model.addAttribute("itemList", itemList);
  60. return "item_list";
  61. }
  62. }

ItemInfo.java:

  1. /**
  2. *
  3. */
  4. package com.sikiedu.bean;
  5. /**
  6. *
  7. *
  8. * @author Lijiang
  9. *2019年5月3日
  10. */
  11. public class ItemInfo {
  12. private String item_id;
  13. private String item_name;
  14. private String item_type;
  15. private String item_price;
  16. public ItemInfo(String item_id, String item_name, String item_type, String item_price) {
  17. super();
  18. this.item_id = item_id;
  19. this.item_name = item_name;
  20. this.item_type = item_type;
  21. this.item_price = item_price;
  22. }
  23. public String getItem_id() {
  24. return item_id;
  25. }
  26. public void setItem_id(String item_id) {
  27. this.item_id = item_id;
  28. }
  29. public String getItem_name() {
  30. return item_name;
  31. }
  32. public void setItem_name(String item_name) {
  33. this.item_name = item_name;
  34. }
  35. public String getItem_type() {
  36. return item_type;
  37. }
  38. public void setItem_type(String item_type) {
  39. this.item_type = item_type;
  40. }
  41. public String getItem_price() {
  42. return item_price;
  43. }
  44. public void setItem_price(String item_price) {
  45. this.item_price = item_price;
  46. }
  47. @Override
  48. public String toString() {
  49. return "ItemInfo [item_id=" + item_id + ", item_name=" + item_name + ", item_type=" + item_type
  50. + ", item_price=" + item_price + "]";
  51. }
  52. }

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>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="list.do">welcome to springmvc</a>
  11. <form action="${pageContext.request.contextPath}/list.do" method="get">
  12. <input type="submit" value="提交:list.do">
  13. </form><br>
  14. <form action="${pageContext.request.contextPath}/mylist.do" method="post">
  15. <input type="submit" value="提交:mylist.do">
  16. </form><br>
  17. <form action="${pageContext.request.contextPath}/forwardString.do">
  18. <input type="submit" value="提交:forwardString.do">
  19. </form><br>
  20. <form>
  21. <input type="text" value="url末尾输入:redirectString.do">
  22. </form><br>
  23. <form action="${pageContext.request.contextPath}/testList.do">
  24. <input type="submit" value="提交:testList.do">
  25. </form><br>
  26. </body>
  27. </html>

item_list.jsp:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwMzIzMjU2_size_16_color_FFFFFF_t_70 6

运行结果:

20190503153613649.gif

进阶篇:*ssm三大框架整合*

发表评论

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

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

相关阅读