日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Spring教程:SpringMVC表單處理例子

Spring MVC 表單處理例子

下面的例子說明了如何編寫一個簡單的基于 web 的應(yīng)用程序,它利用了使用 Spring 的 Web MVC 框架的 HTML 表單。為了開始使用它,讓我們在恰當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟使用 Spring 的 Web 框架來開發(fā)一個動態(tài)的基于表單的 Web 應(yīng)用程序:

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比江都網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式江都網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋江都地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。

步驟 描述
1創(chuàng)建一個名稱為 HelloWeb動態(tài) Web 項目,并且在已創(chuàng)建的項目的 src 文件夾中創(chuàng)建一個包 com.tutorialspoint。
2將上面提到的 Spring 和其他庫拖拽到文件夾 WebContent/WEB-INF/lib 中。
3com.tutorialspoint 包下創(chuàng)建一個 Java 類 StudentStudentController。
4WebContent/WEB-INF 文件夾下創(chuàng)建 Spring 的配置文件 Web.xmlHelloWeb-servlet.xml
5WebContent/WEB-INF 文件夾下創(chuàng)建名稱為 jsp 的子文件夾。在這個子文件夾下創(chuàng)建視圖文件 student.jspresult.jsp。
6最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容,并導(dǎo)出該應(yīng)用程序,正如下面解釋的一樣。

這里是 Student.java 文件的內(nèi)容:

package com.tutorialspoint;
public class Student {
   private Integer age;
   private String name;
   private Integer id;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public Integer getId() {
      return id;
   }
}

下面是 StudentController.java 文件的內(nèi)容:

關(guān)于@ModelAttribute的使用,

查看 @ModelAttribute注解定義可以看到這個注解可以用在方法和參數(shù)中,將請求參數(shù)綁定到Model對象。

可前往springmvc教程中的該章節(jié)查看詳細(xì)內(nèi)容。

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller
public class StudentController {
   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }   
   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());      
      return "result";
   }
}

在這里,第一個 service 方法 student(),我們已經(jīng)在名稱為 “command” 的 ModelAndView 對象中傳遞一個空的 Student 對象,因為 spring 框架需要一個名稱的 “command” 的對象,如果你在 JSP 文件中使用 標(biāo)簽。所以,當(dāng) student() 方法被調(diào)用時,它返回 student.jsp 視圖。

第二個 service 方法 addStudent() 將調(diào)用 HelloWeb/addStudent URL 中的 POST 方法。你將根據(jù)提交的信息準(zhǔn)備好你的模型對象。最后一個 “result” 視圖會從 service 方法中返回,它將導(dǎo)致呈現(xiàn) result.jsp。

下面是 Spring Web 配置文件 web.xml 的內(nèi)容



    Spring MVC Form Handling

    
        HelloWeb
        
           org.springframework.web.servlet.DispatcherServlet
        
        1
    

    
        HelloWeb
        /
    


下面是另一個 Spring Web 配置文件 HelloWeb-servlet.xml 的內(nèi)容



   

   
      
      
   


下面是 Spring 視圖文件 student.jsp 的內(nèi)容

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>


    Spring MVC Form Handling



Student Information

Name
Age
id

下面是 Spring 視圖文件 result.jsp 的內(nèi)容

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>


    Spring MVC Form Handling



Submitted Student Information

Name ${name}
Age ${age}
ID ${id}

最后,下面是包含在你的 web 應(yīng)用程序中的 Spring 和其他庫的列表。你僅僅需要將這些文件拖拽到 WebContent/WEB-INF/lib 文件夾中。

  • commons-logging-x.y.z.jar

  • org.springframework.asm-x.y.z.jar

  • org.springframework.beans-x.y.z.jar

  • org.springframework.context-x.y.z.jar

  • org.springframework.core-x.y.z.jar

  • org.springframework.expression-x.y.z.jar

  • org.springframework.web.servlet-x.y.z.jar

  • org.springframework.web-x.y.z.jar

  • spring-web.jar

一旦你完成了創(chuàng)建源代碼和配置文件后,導(dǎo)出你的應(yīng)用程序。右鍵單擊你的應(yīng)用程序,并且使用 Export > WAR File 選項,并且在 Tomcat 的 webapps 文件夾中保存你的 HelloWeb.war 文件。

現(xiàn)在啟動你的 Tomcat 服務(wù)器,并且確保你能夠使用標(biāo)準(zhǔn)的瀏覽器訪問 webapps 文件夾中的其他 web 頁面。現(xiàn)在嘗試訪問該 URL http://localhost:8080/HelloWeb/student。如果你的 Spring Web 應(yīng)用程序一切都正常,你應(yīng)該看到下面的結(jié)果:

在提交必需的信息之后,單擊提交按鈕來提交這個表單。如果你的 Spring Web 應(yīng)用程序一切都正常,你應(yīng)該看到下面的結(jié)果:


當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Spring教程:SpringMVC表單處理例子
分享URL:http://www.dlmjj.cn/article/dhhojcd.html