发表于: 2017-06-27 22:08:07
3 1124
今天完成的事情:
学习StringMVC,学习RequestMapping映射提交的GET和POST的用法,和之前学习的HTML融会贯通
<form action="name" method="POST">这段代码是表示以POST提交的方式把数据提交到name页面
贴点代码:
package com.gaussic.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Created by dzkan on 2016/3/8.
*/
//@Controller注解:采用注解的方式,可以明确地定义该类为处理请求的Controller类
@Controller
public class MainController {
// @RequestMapping()用于定义一个请求映射,value为请求的url,值为 / 说明,
// 该请为求首页请求,method用以指定该请求类型,一般为get和post;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(ModelMap model) {
return "zhuche";
// 进入zhuche页面
}
// 输入xxx/page/name/age可以把name和age传入name页面
@RequestMapping(value = "/page/{name}/{age}", method = RequestMethod.GET)
public String getName(ModelMap model, @PathVariable("name") String name, @PathVariable("age") int age) {
model.put("name", name);
model.put("age", age);
return "name";
}
// 输入xxx/page?name=张三/age=20可以把name和age传入result页面
@RequestMapping(value = "/result", method = RequestMethod.GET)
public String result(ModelMap model, @RequestParam("name") String name, @RequestParam("age") int age) {
model.put("name", name);
model.put("age", age);
return "result";
}
// 输入xxx/page?name=张三/age=20可以把name和age传入result页面(同上)
@RequestMapping(value = "/name", method = RequestMethod.POST)
public String name(ModelMap model, @RequestParam("name") String name, @RequestParam("age") int age) {
model.put("name", name);
model.put("age", age);
return "name";
}
// get方式写在地址栏里
// post方式提交不写在地址栏里
// 提交的页面必须是get
@RequestMapping(value = "/add_user", method = RequestMethod.GET)
public String addUser(ModelMap map) {
return "addUser";
}
}
这个是addUser.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>添加用户</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<form action="name" method="POST">
<table border=2 bordercolor="00ffff" width=500 cellpadding=10 cellspacing=0>
<tr>
<th colspan=2>注册表单</th>
<!--colspan是行合并-->
<!--rowspan是列合并-->
</tr>
<tr>
<td>名字</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="number" name="age"></td>
</tr>
<tr>
<th colspan=2><input type="reset" value="重设">
<input type="submit" value="注册"></th>
</tr>
</table>
</form>
</body>
</html>这个是name.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>name</title>
</head>
<body>
名字:${name}<br/>
年龄:${age}<br/>
</body>
</html>
明天计划的事情:
学习从网页往数据库里增删查改
遇到的问题:
用GET方式提交数据时,地址栏不显示提交的数据,页面报404,找了很长时间,
发现是<input type="xx" name="xx">中的name写成了value,然后用post
提交报不能应用GET方法的错误(忘记截图了),后来发现是提交数据的页面必须是
GET方法提交,接受数据页面的如果不想显示的话用POST,显示的话用GET
收获:
理解并且实践了RESTful接口
评论