Spring MVC文件配置以及参数传递示例详解

时间:2022-08-25 09:52:23

web.xml文件配置

创建好一个SpringMVC项目后,需要在需要在WB-INF文件夹下配置web.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6.  
  7. <display-name>SpringMVCdemo</display-name>
  8. <welcome-file-list>
  9. <welcome-file>index.jsp</welcome-file>
  10. </welcome-file-list>
  11.  
  12. <!--加载springMVC的配置文件-->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath*:springMVC.xml</param-value>
  16. </context-param>
  17.  
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21.  
  22. <servlet>
  23. <servlet-name>dispatcher</servlet-name>
  24. <!--*核心控制器-->
  25. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  26. <load-on-startup>1</load-on-startup>
  27. </servlet>
  28. <servlet-mapping>
  29. <servlet-name>dispatcher</servlet-name>
  30. <!--请求-->
  31. <url-pattern>*.do</url-pattern>
  32. </servlet-mapping>
  33.  
  34. <!--过滤器,编码格式-->
  35. <filter>
  36. <filter-name>characterEncodingFilter</filter-name>
  37. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  38. <init-param>
  39. <param-name>encoding</param-name>
  40. <param-value>UTF-8</param-value>
  41. </init-param>
  42. </filter>
  43. <filter-mapping>
  44. <filter-name>characterEncodingFilter</filter-name>
  45. <url-pattern>/*</url-pattern>
  46. </filter-mapping>
  47. </web-app>

springMVC.xml文件配置

在src文件夹下创建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:tx="http://www.springframework.org/schema/tx"
  7. xmlns:mvc="http://www.springframework.org/schema/mvc"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context.xsd
  16. http://www.springframework.org/schema/mvc
  17. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  18.  
  19. <!--自动扫描上下文包-->
  20. <context:component-scan base-package="cn.zhc.*"></context:component-scan>
  21.  
  22. <!--自动开启MVC模式注解-->
  23. <mvc:annotation-driven></mvc:annotation-driven>
  24. <!--将请求映射到标注@RequestMapping注解的控制器和处理方法上-->
  25. <mvc:default-servlet-handler></mvc:default-servlet-handler>
  26.  
  27. <!--视图解析器-->
  28. <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  29. <!--前缀后缀-->
  30. <property name="prefix" value="/"></property>
  31. <property name="suffix" value=".jsp"></property>
  32. </bean>
  33. </beans>

第一个SpringMVC实例

index.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>$Title$</title>
  5. </head>
  6. <body>
  7. 哈哈哈哈哈
  8. </body>
  9. </html>

测试类:

  1. package cn.zhc.test;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5.  
  6. @Controller
  7. public class Test {
  8. @RequestMapping("/hello.do")
  9. public String hello(){
  10. System.out.println("hhhhhhhhhhhh");
  11. return "index";
  12. }
  13. }

在项目运行后,在前端页面路径后输入/hello.do,控制台会输出hhhhhhhhhhhh

Spring MVC文件配置以及参数传递示例详解

参数传递

view到controller 四种方式

  1. @RequestMapping("/hello.do")
  2. public String hello(String name){
  3. //路径后加?name= 不加会传null
  4. System.out.println(name);
  5. return "index";
  6. }
  7.  
  8. //Controller方法方法中参数前加@RequestParam进行直接入参
  9.  
  10. @RequestMapping("/hello.do")
  11. public String hello(@RequestParam String name){
  12. //不传参会请求错误400
  13. System.out.println(name);
  14. return "index";
  15. }
  16.  
  17. @RequestMapping("/hello.do")
  18. public String hello(@RequestParam(value = "name" ,required = false) String name){
  19. //required是否需要传参
  20. System.out.println(name);
  21. return "index";
  22. }
  23.  
  24. @RequestMapping(value = "/hello.do",method = RequestMethod.GET,params = "name")
  25. public String hello(String name){
  26. //不传参会请求错误400
  27. System.out.println(name);
  28. return "index";
  29. }

controller到view 三种方式

  1. @RequestMapping("/hello.do")
  2. public ModelAndView hello(){
  3. ModelAndView mv = new ModelAndView();
  4. mv.addObject("name","zhu");//添加模型数据
  5. mv.setViewName("index");//设置视图名称
  6. return mv;
  7. }
  8.  
  9. @RequestMapping("/hello.do")
  10. public String hello(Model model){
  11. model.addAttribute("name","huai");
  12. model.addAttribute("chang");
  13. //在model中若不指定key,则使用默认对象的类型作为key
  14. return "index";
  15. }
  16.  
  17. @RequestMapping("/hello.do")
  18. public String hello(Map<String,Object> map){
  19. map.put("name","lisa");
  20. return "index";
  21. }

总结

到此这篇关于Spring MVC文件配置以及参数传递的文章就介绍到这了,更多相关SpringMVC文件配置参数传递内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_43928469/article/details/115048026