📅  最后修改于: 2020-11-11 06:19:19             🧑  作者: Mango
以下示例说明如何使用Spring MVC Framework编写基于Web的简单Hello World应用程序。首先,让我们拥有一个运行良好的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 Student, StudentController under the com.tutorialspoint package. |
3 | Create view files student.jsp, result.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. |
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;
}
}
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";
}
}
在这里,第一个服务方法student() ,我们在ModelAndView对象中传递了一个空白的Studentobject,名称为“ command”。之所以这样做,是因为如果我们在JSP文件中使用