Post提交与Get提交小结

 狭义讲,这两种方式的区别就是:以何种方式"携带"数据.
get提交,通过url后跟一对或多对键|值的方式传递给请求的页面.
post提交,通过将数据夹带在body区域传递给请求的页面.

表单提交方式(HttpMethod)分为"Get"和"Post".
设置为Get方式,在所请求的url后面附加一串键|值,在服务器端代码可以通过Request.QueryString[]访问.
设置为Post方式,则在服务器端代码可以通过Request.Form[]访问.
直接在IE地址栏中敲url,如http://localhost:9966/WebForm2.aspx"或"http://localhost:9966/WebForm2.aspx?lll=2" 均属于Get方式访问.

注意,Get是Form的默认方法,Get提交字符数量有限,就是因为url长度的限制,2048字节(2KB).


下面的例子演示如何将数据从一个页面提交至另一个页面

[请求页面]
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="WebForm1.aspx.cs"

Inherits="WebDiary.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>无标题页</title>

    <script type="text/javascript">
    function RegisterAccount()
    {
        if (!document.getElementById('agree').checked)
        {
            alert('You can register after you agree with terms!');
            window.event.returnValue=false;
            return false;
        }
        else if(document.getElementById('agree').checked)
        {            
            document.forms('register_member_form').submit();
        }
        else
        {
            alert('Unknown Error!');
        }
    }
    </script>

</head>
<body ms_positioning="GridLayout">
    <form id="register_member_form" method="post" action="WebForm2.aspx" target="_blank">
        <input type="text" name="text" value="text_value">
        <input type="checkbox" id="agree" name="checkbox" value="checkbox_value">
        <input type="hidden" name="hidden"  value="hidden_value"/>
        <input type="image" name="image" value="image_value" />
        <input type="reset" name="reset"  value="reset_value"/>
        <input type="submit" name="sub" value="submit_value" />
        <input type="radio" name="radio" checked/>
        <input type="password" name="password" value="password_value"/>
        <input type="file" name="file" value="file_value"/>
        <input type="button" name="button" class="formSubmit" value="submit"

οnclick="RegisterAccount()" />
    </form>
</body>


--后台代码WebForm1.aspx.cs为默认.

[接收页面]


namespace WebDiary
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private static int i = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                i++;

                Response.Write(i.ToString());
            }

        }
    }
}

--前台页面WebForm2.aspx为默认.


注意,在WebForm1.aspx中必须指定<input type="checkbox" id="agree" name="checkbox" value="checkbox_value">中的name属性,仅有id不行,否则在WebForm2.aspx.cs后台代码中Request.querystring[](对应get) 或 Request.Form[](对应post)无法访问.

    同为input元素,只有type为file、text、radio、checkbox、hidden、password的能读出键值,而submit、

button、Reset、image无法读出键值。并且radio和checkbox必须为checked才可以读出键值.

   补充,submit的name若等于"submit"无法提交表单,所以上例设为了"sub"(原因不清楚).

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`@RequestMapping`是Spring MVC框架中的一个重要注解,用于将HTTP请求映射到控制器的方法上。这个注解用于简化RESTful风格的Web服务开发,使得路由和处理逻辑更加清晰和模块化。下面是一些关于`@RequestMapping`实验小结的关键点: 1. **路径映射**:`@RequestMapping`用于指定一个或多个HTTP请求方法(GET, POST, PUT, DELETE等),以及对应的URL路径。例如,`@RequestMapping(value = "/users", method = RequestMethod.GET)`表示该方法处理所有发送到"/users"路径的GET请求。 2. **请求参数**:可以通过`@RequestParam`或`@PathVariable`注解处理请求参数,如查询参数、路径变量等。如`@RequestParam(name="id") Long userId`会从请求中获取名为"id"的查询参数。 3. **返回类型**:`@ResponseBody`可以用来标记返回值为JSON或XML响应体,而`@ModelAttribute`用于处理表单提交的模型属性。 4. **异常处理**:可以使用`@ExceptionHandler`注解来捕获特定的异常,并提供定制的错误处理。 5. **分组和扫描器**:为了方便管理,可以通过`@RequestMapping`的`@ControllerAdvice`或`@Controller`的`@RequestMapping(basePath = "/api/v1")`来对多个控制器进行分组或定义统一的路径前缀。 相关问题-- 1. `@RequestMapping`如何处理不同类型的HTTP请求? 2. 如何在Spring MVC中使用`@RequestParam`和`@PathVariable`? 3. `@ResponseBody`和`@ModelAttribute`的区别是什么? 4. 如何在Spring MVC中实现全局异常处理? 5. 分组和扫描器在Spring MVC中的应用场景是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值