• SpringMVC最简单配置应用


    一、项目配置

    1.建立java web项目

    2.导入相关jar包

    3.配置web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>SpringMVC</display-name>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- 配置dispatcherServlet -->
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc_servlet.xml</param-value>
        </init-param>
      </servlet>
      
      <servlet-mapping>
         <servlet-name>springmvc</servlet-name>
         <url-pattern>/</url-pattern>
      </servlet-mapping>
      
     <!-- 编码过滤器 -->
     <filter>
       <filter-name>encode</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <async-supported>true</async-supported> <!-- 异步 --> 
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
     </filter>
     
     <filter-mapping>
       <filter-name>encode</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>
      
      
    </web-app>

    4.创建springmvc_servlet.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:util="http://www.springframework.org/schema/util"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 
    
          
      <!-- 使用注解方式完成映射 -->
      <!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
      <context:component-scan base-package="com.etc.controller"/>      
       <mvc:annotation-driven/><!-- 开启注解 -->
       
        <!-- 视图解析器 -->    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"></property>
        </bean>     
    </beans>

    5.创建Controller

    建包com.etc.controller

    建类文件HelloController.java

    package com.etc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
        @RequestMapping(value="/welcome")  //welcome要访问的url地址
        public  String  hello(){
            System.out.println("hello,springmvc");
            return "hello";  //hello是逻辑视图名,和后缀名组合一起构成视图名  /web-inf/jsp/hello.jsp
        }
    
    }

    6.创建视图

    在WEB-INF下创建jsp文件夹,在文件下创建hello.jsp文件

    7.在浏览器输入访问地址

    http://localhost:8080/SpringMVC/welcome

    二、参数传递

    1.前台到后台

    方法一 http://localhost:8080/SpringMVC/welcome?uname="333"

    @RequestMapping(value="/welcome")  //welcome要访问的url地址
        public  String  hello(String uname){//此时方法参数与传来参数名称一致
            System.out.println("hello,springmvc"+uname);
            return "hello";  //hello是逻辑视图名,和后缀名组合一起构成视图名  /web-inf/jsp/hello.jsp
        }

    方法二  http://localhost:8080/SpringMVC/welcome2?username="333"

    @RequestMapping(value="/welcome2")  //welcome要访问的url地址
        public  String  hello2(@RequestParam(value="username",required=false)String uname){
            //此时方法参数value="username"与传来参数名称一致,required=false不传参uname为null
            //required=false不传参出错
            System.out.println("hello,springmvc"+uname);
            return "hello";  //hello是逻辑视图名,和后缀名组合一起构成视图名  /web-inf/jsp/hello.jsp
        }

    2.后台向前台传参

    方法一    http://localhost:8080/SpringMVC/welcome3?uname="333"

    @RequestMapping(value="/welcome3")  
        public  String hello3(String  uname,Model model){
            System.out.println("页面过来的参数是:"+uname);
            model.addAttribute("username","张三");
            return "hello";
        }

    前台获取

    <body>
        后台传递的参数:${username} <br>
      </body>

    方法二 http://localhost:8080/SpringMVC/welcome4?uname="333"

    @RequestMapping(value="/welcome4")  
        public  String hello4(String  uname,Map<String,Object> map){
            System.out.println("页面过来的参数是:"+uname);
            map.put("username","张三");
            return "hello";
        }

    前台同上

    方法三 http://localhost:8080/SpringMVC/welcome4?uname="333"

    @RequestMapping(value="/welcome5")  
        public  ModelAndView hello5(String uname){
            ModelAndView mv=new ModelAndView();
            Users user=new Users();
            user.setId(1);
            user.setName("李红");
            mv.addObject("username",uname);
            mv.addObject(uname);
            mv.addObject("user", user);
            
            mv.setViewName("hello");
            return mv;
    
        }

    前台

    <body>
        后台传递的参数:${username} <br>
        ${string}<br>
        ${user.name }
      </body>
  • 相关阅读:
    在Swift中定义属于自己的运算符
    计算型属性 vs 懒加载
    swift- mutating
    什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
    微信授权登录-微信公众号和PC端网站
    PHP实现购物车的思路和源码分析
    PHP实现图片的等比缩放和Logo水印功能示例
    PHP实现IP访问限制及提交次数的方法详解
    Laravel 队列发送邮件
    laravel 定时任务通过队列发送邮件
  • 原文地址:https://www.cnblogs.com/wuxinyiwu/p/7552013.html
Copyright © 2020-2023  润新知