当前位置:编程学习 > JAVA >>

Struts2和GET/POST的问题

项目里原来没有Struts,纯用Servlet来处理请求,这样get和post就分工很明确,通过点击url和js跳转的请求一律用doGet,通过表单提交的一律用doPost。

比如点击进入某update的页面,doGet先取数据放到页面的表单上,然后修改后表单提交用doPost处理,这样一个Servlet就能处理完整一个功能。

后来打算用struts2,但是看了下,好像struts2并没有对post和get分别进行处理? --------------------编程问答-------------------- struts2 是根据表单的 action = "" 去struts2.xml配置文件里去寻找相应的action处理请求的  --------------------编程问答-------------------- struts2也保留了这两种方式的呀 --------------------编程问答-------------------- 在struts.xml中来搞定这个 --------------------编程问答--------------------
引用 1 楼 qdcaijunjun 的回复:
struts2 是根据表单的 action = "" 去struts2.xml配置文件里去寻找相应的action处理请求的


就是说,get和post需要手工指定控制器调用哪个action来处理请求? --------------------编程问答-------------------- --------------------编程问答-------------------- <result name="suc" type="redirect">/suc.jsp</result>

配置文件中有个type属性,redirect:就相当于get提交。不写就是post提交 --------------------编程问答-------------------- 请求当然有处理咯。不过struts 是在配置文件里面的。 --------------------编程问答-------------------- struts.xml文件做处理了 --------------------编程问答-------------------- --------------------编程问答-------------------- 同学,问这个问题说明你对struts2根本就不了解啊。

S2是基于拦截器的。你在WEB。XML中配置的拦截器,当你的请求过来的时候,他便会拦截到。至于S2的action,他并不是servlet类,他只是一个纯POJO,明白意思吗?就是一个普通的JavaBean。

拦截器会根据你在struts.xml中的配置的action的  class  method 两个属性 找到相关的类,以及相关的方法。

用反射机制,调用这个方法。明白了吗? --------------------编程问答--------------------
引用 10 楼  的回复:
同学,问这个问题说明你对struts2根本就不了解啊。

S2是基于拦截器的。你在WEB。XML中配置的拦截器,当你的请求过来的时候,他便会拦截到。至于S2的action,他并不是servlet类,他只是一个纯POJO,明白意思吗?就是一个普通的JavaBean。

拦截器会根据你在struts.xml中的配置的action的  class  method 两个属性 找到相关的类,以及相……


这我知道,我是说拦截器貌似并不不认GET和POST --------------------编程问答-------------------- GET和POST的不同在于GET请求数据没有POST复杂,参数是加在URL后面的,安全性不高
Struts2参数是从底层发过去,直接映射到POJO的,封装了POST和GET,而且增加了多种请求方式,比较好 --------------------编程问答-------------------- struts2 的处理都是默认为post提交的!除非你自己修改为get! --------------------编程问答--------------------
引用 12 楼 yueprince5 的回复:
GET和POST的不同在于GET请求数据没有POST复杂,参数是加在URL后面的,安全性不高
Struts2参数是从底层发过去,直接映射到POJO的,封装了POST和GET,而且增加了多种请求方式,比较好


这么说是没错,但是这样的话,打开页面和提交表单就需要有两个pojo来分别出来打开和提交,而Servlet就只用一个。 --------------------编程问答-------------------- 现在的问题不是struts2的处理方式好不好,而是,现有的系统是Servlet的,要移植到struts2,有没有简单的办法,而不需要把每个Servlet都按照doGet和doPost拆分成两个Action --------------------编程问答-------------------- 你只需要在对应的struts2 的Action的方法中 进行判断封装代码
及通过request对象来判断发送的方式是get还是post 然后调用不同代码 --------------------编程问答-------------------- struts2没有面向servlet编程哦。。。你不要考虑get post ,如果要修改的话,在jsp from 中修改属性即可啊

action直接通过 其属性获取页面中的值 啊。不要想的太多 --------------------编程问答-------------------- 可以用request.getMethod();返回值就是返回请求方式 (POST/GET...)    --------------------编程问答-------------------- <form action="login.action" method="post"> method 决定用post还是get

引用
<result name="suc" type="redirect">/suc.jsp</result>

配置文件中有个type属性,redirect:就相当于get提交。不写就是post提交

--------------------编程问答-------------------- xml处理了 --------------------编程问答-------------------- 没人回答对问题 --------------------编程问答-------------------- 我是没找到通过httpmethod区分请求映射到action中方法的方法,你可以将get和post都映射到action中的同一个方法,然后在里面判断一下调用不同的方法。 --------------------编程问答-------------------- 还是springmvc好点不但能通过post和get区分请求还能用put和delete --------------------编程问答-------------------- 好得不是一般多! --------------------编程问答-------------------- 可以使用HttpServletRequest的getMethod()方法获取到是什么请求方式,再根据获取到的请求方式去做相应的操作
--------------------编程问答--------------------
引用 19 楼 woshishui6501 的回复:
<form action="login.action" method="post"> method 决定用post还是get

引用
<result name="suc" type="redirect">/suc.jsp</result>

配置文件中有个type属性,redirect:就相当于get提交。不写就是post提交


++1 --------------------编程问答-------------------- 首先,即使你用server来处理修改页面 也可以用doPost  接着 你用struts框架来处理 那么你页面的method=“XX” 不管你用什么方式来提交struts的配置文件都会帮你完成请求,不过建议都用post  你用server 改变过来 全部都用post 提交都没有关系  不需要像你想的那样 还要分两个action 来接受两个不同的提交方式! --------------------编程问答--------------------
引用 6 楼 xinghui_liu 的回复:
<result name="suc" type="redirect">/suc.jsp</result>

配置文件中有个type属性,redirect:就相当于get提交。不写就是post提交

谬论,这个type如果不写,对应的是http里的forward,如果写了对应的是redirect,即302跳转,关get和post什么事? --------------------编程问答-------------------- request.getMethod();

lz看看HttpServlet的原码就知道了,doPost doGet之类的方法是怎么调用的


HttpServlet service方法
    /**
     * Receives standard HTTP requests from the public
     * <code>service</code> method and dispatches
     * them to the <code>do</code><i>XXX</i> methods defined in 
     * this class. This method is an HTTP-specific version of the 
     * {@link javax.servlet.Servlet#service} method. There's no
     * need to override this method.
     *
     * @param req   the {@link HttpServletRequest} object that
     *                  contains the request the client made of
     *                  the servlet
     *
     * @param resp  the {@link HttpServletResponse} object that
     *                  contains the response the servlet returns
     *                  to the client                                
     *
     * @exception IOException   if an input or output error occurs
     *                              while the servlet is handling the
     *                              HTTP request
     *
     * @exception ServletException  if the HTTP request
     *                                  cannot be handled
     * 
     * @see javax.servlet.Servlet#service
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String method = req.getMethod();

        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }

        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);

        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);
            
        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);        
            
        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);
            
        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);
            
        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);
            
        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);
            
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }
--------------------编程问答-------------------- 还有那些配置<result name="suc" type="redirect">/suc.jsp</result>你们确定这个是干嘛的么?
这是跳转到结果视图,分别对应
request.getRequestDispatcher("").forward(arg0, arg1)
response.sendRedirect(arg0)
也就是28楼说的,跟提交表单有什么关系?

决定是post还是get的是  form 的 method 属性 --------------------编程问答-------------------- 真是能扯。。。
楼主知道servlet中有doGet或者doPost方法。
那一个普通的超链接进入servlet中,你觉得是应该是doGet还是doPost方法呢,当然是doGet方法,因为无论怎么样,超链接都是get方式传参。
那一个表单你用get或者post方法,只取决于form 的method属性。。。
struts.xml只是分发跳转的方式而已。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,