봄싹2009.12.08 11:13
필터를 이용해서 트릭을 써야 하나 싶었더니..
역시나 발빠른 스프링 진영에서 ... ㅎ
이미 만들어 놓아서 ... 수고를 덜어주었네요 ;;ㅎㅎ

스프링 만쉐~~


   <filter>
            <filter-name>httpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
   
    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


web.xml 에 등록해준다..

그리고 jsp 에선
<form:form method="delete" > xxxxxxxxxxx </form:form>
쓰던가 ..

아님..

  <form action="/sandbox/post" method="post">
            <input type="submit"/>
        </form>
        <form action="/sandbox/put" method="post">
            <input type="text" name = "_method" value="put">
            <input type="submit"/>
        </form>
        <form action="/sandbox/delete" method="post">
            <input type="text" name = "_method" value="delete">
            <input type="submit"/>
        </form>

이렇게 쓰던가.. 빨간 놈이 핵심이다;;

HiddenHttpMethodFilter  소스의 일부분이다..

public class HiddenHttpMethodFilter extends OncePerRequestFilter {

    /** Default method parameter: <code>_method</code> */
    public static final String DEFAULT_METHOD_PARAM = "_method";

    private String methodParam = DEFAULT_METHOD_PARAM;


    /**
     * Set the parameter name to look for HTTP methods.
     * @see #DEFAULT_METHOD_PARAM
     */
    public void setMethodParam(String methodParam) {
        Assert.hasText(methodParam, "'methodParam' must not be empty");
        this.methodParam = methodParam;
    }

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        String paramValue = request.getParameter(this.methodParam);
        if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
            String method = paramValue.toUpperCase(Locale.ENGLISH);
            HttpServletRequest wrapper = new HttpMethodRequestWrapper(method, request);
            filterChain.doFilter(wrapper, response);
        }
        else {
            filterChain.doFilter(request, response);
        }
    }


이상.. 귀찮은 포스팅 끝.!!
Posted by is윤군

댓글을 달아 주세요

  1. 짝짝짝

    2009.12.08 11:51 [ ADDR : EDIT/ DEL : REPLY ]