<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                Spring框架有一套表單標簽庫,可在jsp頁面(只能在.jsp中使用)中渲染HTML元素標簽。 :-: ![](https://img.kancloud.cn/d3/f5/d3f51e1f52f1cf680dfd15072555652d_1205x409.jpg) Spring提供的全部表單標簽 上面這些Spring標簽庫,它們與普通的HTML元素是一樣,只是添加了一些Spring自身提供的屬性,簡化了編碼。 <br/> [TOC] # 1. Spring表單標簽使用步驟 **1. controller層** ```java @Controller public class UserController { /** * 1. 進入表單標簽所在的 user.jsp 頁面時,必須綁定一個User對象,綁定方式可以使用 @ModelAttribute("user") * 、或者Model.addAttribute("user", new User())、或者ModelAndView.addObject("user", new User()) */ @RequestMapping(value = "/touser.html", method = RequestMethod.GET) public String toUserPage(@ModelAttribute("user") User user) { user.setName("姓名"); user.setPassword("密碼"); user.setNickname("別名"); user.setAvator("頭像"); //2. 必須是進入表單標簽所在的 user.jsp 頁面 return "user"; } /** * 3. 這個是表單提交地址,也必須綁定User對象,并且綁定的命名也必須是user */ @RequestMapping(value = "/user/insert2", method = RequestMethod.POST) public String insert2(@ModelAttribute("user") User user) { System.out.println(user); return ""; } } ``` **2. 視圖層** *`webapp/WEB-INF/views/user.jsp`* ```html <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!-- 使用Spring表單標簽需要引入 taglib 庫 --> <%@ taglib prefix="fm" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>USER</title> </head> <body> <!-- modelAttribute: 與controller層中注解@ModelAttribute("user")參數命名一致 如果沒有指定,則默認提供common參數,如果在controller中沒有找到common參數則報錯 --> <fm:form method="post" modelAttribute="user" action="${pageContext.request.contextPath }/user/insert2"> <!-- path: 相當于name屬性,必須與實體類中成員變量名同名, 并且實體類中必須提供變量的Setter方法,因為Spring是根據Setter進行綁定的 --> <fm:input path="name" /> <fm:password path="password" /> <fm:input path="nickname" /> <fm:input path="avator" /> <input type="submit" value="提交" /> </fm:form> </body> </html> </body> </html> ``` **3. 測試** (1)啟動項目后訪問 http://localhost:8080/mvc/touser.html 進入 user.jsp 頁面,得到結果如下。 ![](https://img.kancloud.cn/72/46/72468ca0f6b18024cda2f712c500c51e_1405x166.jpg) (2)填寫表單后提交表單,會將數據傳遞到后端。 ![](https://img.kancloud.cn/5f/6a/5f6a3e5341de836007203a5a3a0c29b3_1394x178.jpg) <br/> # 2. 表單省略`action`屬性 ```html <fm:form method="post" modelAttribute="user"> ``` 表單是可以省略`action`屬性的,條件是:進入 user.jsp 頁面的 url 與 提交表單的 url 一樣時,就可以省略`action`屬性。 <br/> 假如進入 user.jsp 頁面的 url 為: ```java @RequestMapping(value = "/adduser.html", method = RequestMethod.GET) ``` 表單提交的url為: ```java @RequestMapping(value = "/adduser.html", method = RequestMethod.POST) ``` 當 url 地址一樣時,Spring 會根據 method 自動匹配到正確的url地址。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看