javascript
spring mvc入门示例教程-尊龙游戏旗舰厅官网
以下示例演示如何编写一个简单的基于web的应用程序,它使用spring web mvc框架使用html表单。 首先使用eclipse ide,并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:
完整的项目文件结构如下所示 -
student.java文件中的代码内容 -
package com.yiibai.springmvc;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;} }java
studentcontroller.java 文件中的代码内容 -
package com.yiibai.springmvc;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";} }java
这里的第一个服务方法student(),我们已经在modelandview对象中传递了一个名为“command”的空对象,因为如果在jsp中使用
第二个服务方法addstudent()将在 urlhelloweb/addstudent上的post方法提交时调用。将根据提交的信息准备模型对象。最后,将从服务方法返回“result”视图,这将最终渲染result.jsp视图。
student.jsp文件的内容如下所示 -
<%@ page contenttype="text/html; charset=utf-8" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>student information
html
result.jsp文件的内容如下 -
<%@ page contenttype="text/html; charset=utf-8" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>提交的学生信息如下 -
名称: | ${name} |
年龄: | ${age} |
编号: | ${id} |
html
完成创建源和配置文件后,导出应用程序。 右键单击应用程序,并使用导出> war文件选项,并将 formhandling.war 文件保存在tomcat的webapps文件夹中。或者直接右键选择“run as -> run on server”。
启动tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。现在尝试url => http://localhost:8080/formhandling/student ,如果spring web应用程序没有问题,那么应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果spring web应用程序没有问题,应该看到以下结果:
原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring_mvc/springmvc_form_handling.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的spring mvc入门示例教程--表单处理的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: