zoukankan      html  css  js  c++  java
  • spring mvc 参数传递的三种方式

    springmvc.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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        <!-- spring mvc 注解驱动 -->
        <mvc:annotation-driven />
        <!-- 扫描器 -->
        <context:component-scan base-package="com" />
    
        <!-- 配置视图 解析器 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- 前缀和后缀 -->
            <property name="prefix" value="/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>

    controller

    package com.stone.controller;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.servlet.http.HttpServletRequest;
    
    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 com.stone.bean.UserInfo;
    
    /**
     * user/list.do user/add.do
     */
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @RequestMapping("/add1.do")
        public String add(HttpServletRequest request) {
            String userNum = request.getParameter("userNum");
            String passWord = request.getParameter("passWord");
            System.out.println("userNum:" + userNum + "   passWord:" + passWord);
            return "user_add1";
        }
    
        @RequestMapping("/add2.do")
        public String add2(
                @RequestParam(value = "number", defaultValue = "123") String userNum,
                String passWord) {
            System.out.println("userNum:" + userNum + "   passWord:" + passWord);
            return "user_add1";
        }
    
        @RequestMapping("/add3.do")
        public String add3(UserInfo user) {
            System.out.println(user);
            return "user_add1";
        }
    
        @RequestMapping("/add4.do")
        public String add4(Integer userId, Date userTime) {
            System.out.println(userId);
            System.out.println(userTime);
            return "user_add1";
        }
    
        /**
         * 时间属性编辑器
         */
    //    @InitBinder
    //    public void initBinder(ServletRequestDataBinder bin) {
    //        SimpleDateFormat dateFormat = new SimpleDateFormat(
    //                "yyyy-MM-dd HH:mm:ss");
    //        CustomDateEditor cust = new CustomDateEditor(dateFormat, true);
    //        bin.registerCustomEditor(Date.class, cust);
    //    }
    }

    jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>hello</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    
    </head>
    
    <body>
        <h1>用户信息添加</h1>
        <form action="user/add1.do" method="post">
            账号:<input name="userNum" type="text" /> 密码:<input name="passWord"
                type="text" /> <input type="submit" />
        </form>
        <h1>用户信息添加</h1>
        <form action="user/add2.do" method="post">
            账号:<input name="number" type="text" /> 密码:<input name="passWord"
                type="text" /> <input type="submit" />
        </form>
        <h1>用户信息添加</h1>
        <form action="user/add3.do" method="post">
            账号:<input name="userId" type="text" /><br /> 姓名:<input
                name="userName" type="text" /> 性别:<input name="userSex" type="text" />
                兴趣爱好:
                <input type="checkbox" name="userxq" value="LOL"/>LOL
                <input type="checkbox" name="userxq" value="英雄"/>英雄
                <input type="checkbox" name="userxq" value="DOTA"/>DOTA<br/>
                时间:<input name="userTime" type="text" />
            <input type="submit" />
        </form>
        <h1>用户信息添加</h1>
        <form action="user/add4.do" method="post">
            账号:<input name="userId" type="text" /><br />
                时间:<input name="userTime" type="text" />
            <input type="submit" />
        </form>
    </body>
    </html>

    java bean

    package com.stone.bean;
    
    import java.util.Arrays;
    import java.util.Date;
    
    import org.springframework.format.annotation.DateTimeFormat;
    
    public class UserInfo {
    
        private Integer userId;
        private String userName;
        private String userSex;
        private String[] userxq;
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date userTime;
    
        @Override
        public String toString() {
            return "UserInfo [userId=" + userId + ", userName=" + userName
                    + ", userSex=" + userSex + ", userxq="
                    + Arrays.toString(userxq) + ", userTime=" + userTime + "]";
        }
    
        public Date getUserTime() {
            return userTime;
        }
    
        public void setUserTime(Date userTime) {
            this.userTime = userTime;
        }
    
        public String[] getUserxq() {
            return userxq;
        }
    
        public void setUserxq(String[] userxq) {
            this.userxq = userxq;
        }
    
        public Integer getUserId() {
            return userId;
        }
    
        public void setUserId(Integer userId) {
            this.userId = userId;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserSex() {
            return userSex;
        }
    
        public void setUserSex(String userSex) {
            this.userSex = userSex;
        }
    
    }
  • 相关阅读:
    乱七八糟想到什么记什么1
    ubuntu下 安装python module的步骤 分类: python Module 2013-10-12 13:58 624人阅读 评论(0) 收藏
    python中文编码转换 分类: python基础学习 python 小练习 2013-10-11 17:22 331人阅读 评论(0) 收藏
    from ....import导入其他路径的模块 分类: python基础学习 2013-10-11 15:13 315人阅读 评论(0) 收藏
    贪婪 vs 不贪婪 分类: 正则表达式 2013-10-09 15:00 290人阅读 评论(0) 收藏
    删除列表元素时需注意的问题 分类: python基础学习 python 小练习 2013-10-09 14:02 293人阅读 评论(0) 收藏
    #小练习 输出1到100的质数 分类: python 小练习 2013-10-08 17:45 319人阅读 评论(0) 收藏
    Linux下python升级至2.7步骤 分类: python基础学习 python下载 2013-09-29 11:24 4266人阅读 评论(2) 收藏
    实例、类、父类的关系判断 分类: python基础学习 2013-09-27 10:47 614人阅读 评论(0) 收藏
    #小练习 替换文件某行内容 分类: python 小练习 python Module 2013-09-26 11:10 269人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/stono/p/4483983.html
Copyright © 2011-2022 走看看