springmvc 中前後臺傳值

1、前臺往後臺傳值:

 login.jsp輸入框設置,主要是name屬性

 <h1>登錄頁面</h1>

    <form method="post" action="">

        用戶名:<input type="text" name="userName"><br>

        密&nbsp;碼:<input type="text" name="password"><br>

        <input type="submit">

    </form>


loginController.java 中獲取(兩種方式):

@RequestMapping(value="/add",method=RequestMethod.GET)

public String sayHello(@RequestParam("userName") String username,@RequestParam("password") String password,Model model) {


        List<User> userList = new ArrayList<User>();

        User user = new User();

        user.setUserName(username);

        user.setPassword(password);

        user.setAge(password);

        userList.add(user);

        model.addAttribute("users",userList);


        return "user";


    }

    

@RequestMapping(method = RequestMethod.POST)

    public String sayHello1(String userName,String password,Model model) {


        List<User> userList = new ArrayList<User>();

        User user = new User();

        user.setUserName(userName);

        user.setPassword(password);

        user.setAge(password);

        userList.add(user);

        model.addAttribute("users",userList);


        return "user";


    }

2、後臺往前臺傳值:

放到model裏面即可,前臺根據key可獲取到value,代碼如下:

  <h2 style="font:"Trebuchet MS", arial, sans-serif ">用戶列表</h2>

    <c:forEach items="${users}" var="user">

        用戶名:${user.userName}

        性別:${user.sex}

        年齡:${user.age}

        電話:${user.tel} <br/>

    </c:forEach>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章