SpringMVC.入门篇《二》form表单

项目工程结构:

SpringMVC.入门篇《二》form表单-LMLPHP

在《springmvc入门篇一.HelloWorld》基础上继续添加代码,新增:FormController.java,form.jsp,formResult.jsp 三个文件,并修改了index.jsp文件

FormController.java

 package com.charles.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import com.charles.entity.Client; /**
* <p>Type: FormController</p>
* <p>Description: Form表单控制层</p>
* @author baitang.<gy03554>
* @date 2018年10月14日 下午4:37:08
* @version v1.0.0
*/
@Controller
public class FormController { @RequestMapping(value = "formpage", method = RequestMethod.GET)
public ModelAndView intoFormPage() { return new ModelAndView("form", "command", new Client());
} @RequestMapping(value = "register", method = RequestMethod.POST)
public String register(Model model, Client client) { String cemail = client.getCemail();
String cpwd = client.getCpwd(); System.out.println("客户输入的邮箱是:" + cemail);
System.out.println("客户输入的密码是:" + cpwd); model.addAttribute("cemail", cemail);
model.addAttribute("cpwd", cpwd);
return "formResult";
}
}

form.jsp 文件代码:

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC.入门篇《二》form表单</title>
</head> <body> <center>
<h2>客户注册</h2>
<form:form method="post" action="register"> <table border="1px" cellpadding="0" cellspacing="0">
<tr>
<td><form:label path="cemail">邮箱:</form:label> </td>
<td><form:input path="cemail"/> </td>
</tr>
<tr>
<td><form:label path="cpwd">密码:</form:label> </td>
<td><form:input path="cpwd"/> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
</td>
<!-- <td></td> -->
</tr>
</table> </form:form>
</center>
</body>
</html>

formResult.jsp 代码

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<center>
<h2>注册成功</h2>
<h3>邮箱:${cemail }</h3>
<h3>密码:${cpwd }</h3>
</center>
</body>
</html>

index.jsp 代码

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<a href="hello">进入Hello页面.</a> <br/>
<a href="formpage">进入Form表单页面.</a> </body>
</html>

运行项目,通过浏览器访问:http://localhost:9826/springmvc ,点击:进入Form表单页面, 填写form表单信息,然后注册。

SpringMVC.入门篇《二》form表单-LMLPHP

SpringMVC.入门篇《二》form表单-LMLPHP

SpringMVC.入门篇《二》form表单-LMLPHP

如有问题,欢迎纠正!!!

如有转载,请标明源处:https://www.cnblogs.com/Charles-Yuan/p/9786855.html

04-28 06:32