📜  Spring MVC-列表框示例

📅  最后修改于: 2020-11-11 06:26:08             🧑  作者: Mango


以下示例显示了如何使用Spring Web MVC框架在表单中使用列表框。首先,让我们拥有一个运行良好的Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序。

Step Description
1 Create a project with a name HelloWeb under a package com.tutorialspoint as explained in the Spring MVC – Hello World chapter.
2 Create Java classes User, UserController under the com.tutorialspointpackage.
3 Create view files user.jsp, users.jsp under the jsp sub-folder.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

User.java

package com.tutorialspoint;

public class User {
    
   private String username;
   private String password;
   private String address;
   private boolean receivePaper;
   private String [] favoriteFrameworks;   
   private String gender;
   private String favoriteNumber;
   private String country;
   private String [] skills;
   
   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public boolean isReceivePaper() {
      return receivePaper;
   }
   public void setReceivePaper(boolean receivePaper) {
      this.receivePaper = receivePaper;
   }
   public String[] getFavoriteFrameworks() {
      return favoriteFrameworks;
   }
   public void setFavoriteFrameworks(String[] favoriteFrameworks) {
      this.favoriteFrameworks = favoriteFrameworks;
   }
   public String getGender() {
      return gender;
   }
   public void setGender(String gender) {
      this.gender = gender;
   }
   public String getFavoriteNumber() {
      return favoriteNumber;
   }
   public void setFavoriteNumber(String favoriteNumber) {
      this.favoriteNumber = favoriteNumber;
   }
   public String getCountry() {
      return country;
   }
   public void setCountry(String country) {
      this.country = country;
   }
   public String[] getSkills() {
      return skills;
   }
   public void setSkills(String[] skills) {
      this.skills = skills;
   }
}

UserController.java

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 UserController {

   @RequestMapping(value = "/user", method = RequestMethod.GET)
   public ModelAndView user() {
      User user = new User();      
      user.setFavoriteFrameworks((new String []{"Spring MVC","Struts 2"}));
      user.setGender("M");
      ModelAndView modelAndView = new ModelAndView("user", "command", user);
      return modelAndView;
   }

   @RequestMapping(value = "/addUser", method = RequestMethod.POST)
   public String addUser(@ModelAttribute("SpringWeb")User user, 
      ModelMap model) {
      model.addAttribute("username", user.getUsername());
      model.addAttribute("password", user.getPassword());
      model.addAttribute("address", user.getAddress());
      model.addAttribute("receivePaper", user.isReceivePaper());
      model.addAttribute("favoriteFrameworks", user.getFavoriteFrameworks());
      model.addAttribute("gender", user.getGender());
      model.addAttribute("favoriteNumber", user.getFavoriteNumber());
      model.addAttribute("country", user.getCountry());
      model.addAttribute("skills", user.getSkills());
      return "users";
   }
   
   @ModelAttribute("webFrameworkList")
   public List getWebFrameworkList() {
      List webFrameworkList = new ArrayList();
      webFrameworkList.add("Spring MVC");
      webFrameworkList.add("Struts 1");
      webFrameworkList.add("Struts 2");
      webFrameworkList.add("Apache Wicket");
      return webFrameworkList;
   }
   
   @ModelAttribute("numbersList")
   public List getNumbersList() {
      List numbersList = new ArrayList();
      numbersList.add("1");
      numbersList.add("2");
      numbersList.add("3");
      numbersList.add("4");
      return numbersList;
   }

   @ModelAttribute("countryList")
   public Map getCountryList() {
      Map countryList = new HashMap();
      countryList.put("US", "United States");
      countryList.put("CH", "China");
      countryList.put("SG", "Singapore");
      countryList.put("MY", "Malaysia");
      return countryList;
   }
   
   @ModelAttribute("skillsList")
   public Map getSkillsList() {
      Map skillList = new HashMap();
      skillList.put("Hibernate", "Hibernate");
      skillList.put("Spring", "Spring");
      skillList.put("Apache Wicket", "Apache Wicket");
      skillList.put("Struts", "Struts");
      return skillList;
   }
}

在这里,对于第一个服务方法user() ,我们在ModelAndView对象中传递了一个名称为“ command”的空白User对象,因为如果您使用的是,spring框架会期望一个名称为“ command”的对象。 JSP文件中的标签。因此,当调用user()方法时,它将返回user.jsp视图。

将针对HelloWeb / addUser URL上的POST方法调用第二个服务方法addUser() 。您将根据提交的信息准备模型对象。最后,将从服务方法返回“用户”视图,这将导致呈现users.jsp。

user.jsp

Spring MVC Form Handling
   
   

      

User Information

在这里,我们使用标记,属性为multiple = true,以呈现HTML列表框。例如-


它将呈现以下HTML内容。



users.jsp

Spring MVC Form Handling
   
   

      

Submitted User Information

Username ${username}
Password ${password}
Address ${address}
Subscribed to Newsletter ${receivePaper}
Favorite Web Frameworks
Gender ${(gender=="M"? "Male" : "Female")}
Favourite Number ${favoriteNumber}
Country ${country}
Skills

完成创建源文件和配置文件后,导出应用程序。右键单击您的应用程序,使用“导出”→“ WAR文件”选项,并将HelloWeb.war文件保存在Tomcat的webapps文件夹中。

现在,启动Tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。尝试使用URL- http:// localhost:8080 / HelloWeb / user ,如果Spring Web Application一切正常,我们将看到以下屏幕。

春天列表框形式

提交所需信息后,单击“提交”按钮以提交表单。如果您的Spring Web Application一切正常,您应该看到以下屏幕。

Spring列表框表单结果