Last active
August 26, 2018 08:45
-
-
Save kght6123/9de09f78b1f1246183ad5ac44244b47b to your computer and use it in GitHub Desktop.
smart struts1 tips.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActionServlet extends org.apache.struts.action.ActionServlet { | |
/** | |
* 削除 | |
*/ | |
@Override | |
protected void doDelete( | |
final HttpServletRequest req, | |
final HttpServletResponse resp) throws ServletException, IOException { | |
super.doPost(req, resp);// doPostへ | |
} | |
/** | |
* 作成&更新 | |
*/ | |
@Override | |
protected void doPut( | |
final HttpServletRequest req, | |
final HttpServletResponse resp) throws ServletException, IOException { | |
super.doPost(req, resp);// doPostへ | |
} | |
/** | |
* 読込 | |
*/ | |
@Override | |
public void doGet( | |
final HttpServletRequest req, | |
final HttpServletResponse resp) throws IOException, ServletException { | |
super.doGet(req, resp); | |
} | |
/** | |
* 作成 | |
*/ | |
@Override | |
public void doPost( | |
final HttpServletRequest req, | |
final HttpServletResponse resp) throws IOException, ServletException { | |
super.doPost(req, resp); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebFilter(filterName="HttpMethodOverrideFilter", urlPatterns="/*", servletNames="action") | |
public class HttpMethodOverrideFilter | |
implements Filter | |
{ | |
public HttpMethodOverrideFilter(){} | |
public void init(final FilterConfig config) throws ServletException {} | |
public void destroy(){} | |
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException | |
{ | |
if(request instanceof HttpServletRequest) | |
chain.doFilter(new HttpMethodOverrideServletRequest((HttpServletRequest)request), response); | |
else | |
chain.doFilter(request, response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HttpMethodOverrideServletRequest extends HttpServletRequestWrapper | |
{ | |
private final static String _METHOD = "_method"; | |
private final static String X_METHOD_OVERRIDE = "X-Method-Override"; | |
private final static String X_HTTP_METHOD_OVERRIDE = "X-HTTP-Method-Override"; | |
public HttpMethodOverrideServletRequest(final HttpServletRequest request) | |
{ | |
super(request); | |
} | |
@Override | |
public String getMethod() { | |
final String[] methodOverrideValues = | |
new String[]{ | |
getParameter(_METHOD), | |
getParameter(_METHOD.toUpperCase()), | |
getParameter(X_METHOD_OVERRIDE.toLowerCase()), | |
getParameter(X_METHOD_OVERRIDE.toUpperCase()), | |
getParameter(X_HTTP_METHOD_OVERRIDE.toLowerCase()), | |
getParameter(X_HTTP_METHOD_OVERRIDE.toUpperCase()), | |
getHeader(X_METHOD_OVERRIDE), | |
getHeader(X_METHOD_OVERRIDE.toUpperCase()), | |
getHeader(X_HTTP_METHOD_OVERRIDE), | |
getHeader(X_HTTP_METHOD_OVERRIDE.toUpperCase()), | |
}; | |
for(final String methodOverrideValue : methodOverrideValues) { | |
if (StringUtils.isNotEmpty(methodOverrideValue)) | |
return methodOverrideValue; | |
} | |
return super.getMethod(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment