大数据正式33

大数据正式33

SpringMVC

  • Servlet缺点
    1. 一个Servlet有8行配置文件
    2. 一个Servlet只能处理一个业务
    3. 一张表可能有4+n个Servlet,所以会有32+n行配置文件
    4. 获取属性只能得到字符串类型,需要自己转换类型
    5. 获取参数只能是request.getParameter()
  • SpringMVC优点
    1. 不管工程中有多少servlet,只需要配置一个servlet分发器即可
    2. springmvc中使用controller替代一个controller
    3. 获取参数的类型支持自动装换,而且支持对象的自动封装
    4. SpringMvc有多种获取参数的方式

SprigMVC入门案例

  • jar包
  • 项目结构
  • 整体流程
  • 代码

    • web.xml

      
      
          
          
              springmvc
              
              org.springframework.web.servlet.DispatcherServlet
              
              
                  contextConfigLocation
                  classpath:/applicationContext-mvc.xml
              
          
          
              springmvc
              
              *.action
          
          
              index.jsp
          
      
      
    • applicationContext-mvc.xml

      
      
          
          
          
          
      
          
      
      
    • com.peng.controller

      • HelloController

        package com.peng.controller;
        
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        
        import org.springframework.web.servlet.ModelAndView;
        import org.springframework.web.servlet.mvc.Controller;
        
        public class HelloController implements Controller {
        
            public ModelAndView handleRequest(HttpServletRequest request,
                    HttpServletResponse response) throws Exception {
                ModelAndView modelAndView = new ModelAndView();
                modelAndView.setViewName("index.jsp");
                modelAndView.addObject("msg", "hello");
                return modelAndView;
            }
        }
        
    • index.jsp

      <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
      <%
          String path = request.getContextPath();
          String basePath = request.getScheme() + "://"
                  + request.getServerName() + ":" + request.getServerPort()
                  + path + "/";
      %>
      
      
      
      
      
      
      My JSP 'index.jsp' starting page
      
      
      
      
      
      
      
      
      
          This is my JSP page.
          

      ${msg}

  • 访问名称
    • http://localhost/SpringMVC_inroom/hello.action(注:端口为80)
  • 执行结果

注解形式的SpringmMVC

package com.peng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    @RequestMapping("/hello.action")
    public String helloAction(Model model) {
        model.addAttribute("msg", "hello,world!");
        return "index";
    }
}



    
    
    
    
    
    
    
    
        
        
    

添加前后缀的SpringMVC(applicationContext-mvc.xml)



    
    

解决乱码--过滤器(web.xml)


    
        characterFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        characterFilter
        /*
    

数据的获取

  • HttpServletRequest对象的getParameter方法

    package com.peng.controller;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class UserController {
        @RequestMapping("/showAddUser.action")
        public String showAddUser() {
            return "addUser";
        }
    
        @RequestMapping("/addUser.action")
        public String addUser(HttpServletRequest request, Model model) {
            String username = request.getParameter("username");
            System.out.println(username);
            model.addAttribute("username", username);
            return "addUser";
        }
    }
    
  • 与表单的name值一样的参数String username

    • 参数名来控制

      package com.peng.controller;
      
      import javax.servlet.http.HttpServletRequest;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      @Controller
      public class UserController {
          @RequestMapping("/showAddUser.action")
          public String showAddUser() {
              return "addUser";
          }
      
          @RequestMapping("/addUser.action")
          public String addUser(String username, Model model) {
              System.out.println(username);
              model.addAttribute("username", username);
              return "addUser";
          }
      }
      
  • bean对象 User

    • 参数名来控制

      package com.peng.controller;
      
      import javax.servlet.http.HttpServletRequest;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      @Controller
      public class UserController {
          @RequestMapping("/showAddUser.action")
          public String showAddUser() {
              return "addUser";
          }
      
          @RequestMapping("/addUser.action")
          public String addUser(User user, Model model) {
              System.out.println(user.username);
              model.addAttribute("username", user.username);
              return "addUser";
          }
      }
      
  • 对象中的对象

    • 参数名来控制

      • dog.name
      • gog.age

        class Dog{
            private String name;
        
            public void setName(String name){
                this.name=name;
            }
        
            public String getName(){
                return this.name;
            }
        }
        

        class User{
            private dog;
        
            public void setDog(Dog dog{
                this.dog=dog;
            }
        
            public Dog getDog(){
                return this.dog;
            }
        }
        

        package com.peng.controller;
        
        import javax.servlet.http.HttpServletRequest;
        
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.RequestMapping;
        
        @Controller
        public class UserController {
            @RequestMapping("/showAddUser.action")
            public String showAddUser() {
                return "addUser";
            }
        
            @RequestMapping("/addUser.action")
            public String addUser(String username, Model model) {
                System.out.println(user.dog.name);
                model.addAttribute("username", user.dog.name);
                return "addUser";
            }
        }
        

日期问题

  • 格式问题

    2017/12/25这个错误;
    2017-12-25这个可以;
    
  • 解决(Controller中)

    @InitBinder
        public void InitBinder(ServletRequestDataBinder srdb) {
            srdb.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
                    new SimpleDateFormat("yyyy-MM-dd"), true));
        }   
    

参数的同名提交

  • String

    a,b,c
    
  • 例子

    @RequestMapping("/addUser.action")
    public String addUser(
            HttpServletRequest request,
            Model model,
            Date date,
            String like) {
        String username = request.getParameter("username");
        System.out.println(username + date.toString() + like);
        model.addAttribute("username", username);
        return "addUser";
    }
    
  • String[]

    {a,b,c}数组形式
    
  • 例子

    @RequestMapping("/addUser.action")
    public String addUser(
            HttpServletRequest request,
            Model model,
            Date date,
            String[] like) {
        String username = request.getParameter("username");
        System.out.println(username + date.toString() + Arrays.toString(like));
        model.addAttribute("username", username);
        return "addUser";
    }
    

参数名称不一致的问题

  • 问题呈现
    • form表单提交过来的name和参数中的name值不相同时--因为要见名知义,稍微改动了参数的名称
  • @RequestParam("hobby")String[] hobbies
  • @RequestParam
    • value--对应form中的name值
    • defaultValue--默认的值
    • require--必须传过来时有参数
  • 例子

    @RequestMapping("/addUser.action")
    public String addUser(
            HttpServletRequest request,
            Model model,
            Date date,
            @RequestParam(value = "like", defaultValue = "啥也不喜欢!") String[] likes) {
        String username = request.getParameter("username");
        System.out.println(username + date.toString() + Arrays.toString(likes));
        model.addAttribute("username", username);
        return "addUser";
    }
    

文件上传

  • 注意的坑
    1. form中的参数
      1. method:post
      2. enctype:multipart/form-data
    2. file文件标签
      1. 必须有name属性
    3. 文件解析器
      1. 必须有id属性
      2. id属性的值必须是:multipartResolver
  • 改三个地方

    1. jsp中

      <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
      
      
      
      Add User
      
      
      
          
      用户名:
      时间:
      上传文件:
      爱好: 篮球 足球 乒乓球
      提交:
    2. 解析器(必须有id和class)

      
          
              
              
              
          
      
    3. 后台

      package com.peng.controller;
      
      import java.io.File;
      import java.io.IOException;
      import java.text.SimpleDateFormat;
      
      import org.apache.commons.io.FileUtils;
      import org.springframework.beans.propertyeditors.CustomDateEditor;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.ServletRequestDataBinder;
      import org.springframework.web.bind.annotation.InitBinder;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.multipart.MultipartFile;
      
      @Controller
      public class UserController {
          @RequestMapping("/showAddUser.action")
          public String showAddUser() {
              return "addUser";
          }
      
          @RequestMapping("/addUser.action")
          public String addUser(@RequestParam MultipartFile file) throws IOException {
              FileUtils.writeByteArrayToFile(new File(
                      "C:\\Users\\Administrator.PC-20160710IJPJ\\Desktop\\haha.png"),
                      file.getBytes());
              System.out.println(file.toString());
              return "addUser";
          }
      
          @InitBinder
          public void InitBinder(ServletRequestDataBinder srdb) {
              srdb.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
                      new SimpleDateFormat("yyyy-MM-dd"), true));
          }
      }
      

请求和重定向

  • 默认是转发
  • 重定向--不能直接写界面,尤其是WEB-INF下的界面

    return "redirect:请求"
    return "redirect:addUser.action"
    

补充

你可能感兴趣的:(达内实训,大数据学习痕迹)