📅  最后修改于: 2020-11-11 06:22:23             🧑  作者: Mango
以下示例描述了如何使用Spring Web MVC框架在表单中使用单个复选框。首先,让我们拥有一个运行良好的Eclipse IDE,并考虑以下步骤,以使用Spring Web Framework开发基于动态表单的Web应用程序。
Step | Description |
---|---|
1 | Create a project with a name HelloWeb under a package com.tutorialspointas explained in the Spring MVC – Hello World Example chapter. |
2 | Create Java classes User, UserController under the com.tutorialspointpackage. |
3 | Create a view files user.jsp, users.jsp under 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. |
package com.tutorialspoint;
public class User {
private String username;
private String password;
private String address;
private boolean receivePaper;
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;
}
}
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 UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ModelAndView user() {
return new ModelAndView("user", "command", new User());
}
@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());
return "users";
}
}
在这里,对于第一个服务方法user(),我们在ModelAndView对象中传递了一个名称为“ command”的空白User对象,因为如果您使用的是