com.j2mvc.framework.dispatcher.DispatcherServlet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2mvc-framework-web Show documentation
Show all versions of j2mvc-framework-web Show documentation
强烈建议使用J2mvc 2.1以后的版本。
version 2.1.01
1.优化路径跳转,Servlet和Filter方式的路径设置
2.优化内存销毁
3.更换JSON依赖包
4.优化接收方法RequestMethod,封装不同ContentType格式提交
封装JSON,XML数据提交模块
JSON请求示例
{
"id":"JSON134851",
"title":"JSON提交的标题",
"price":65.1,
"stock":100
}
XML请求示例
<!DOCTYPE root [
<!ELEMENT root ANY>
<!ATTLIST Product SSN ID #REQUIRED>]>
<root>
<Product SSN='id'>XMLID12354</Product>
<Product SSN='title'>XML提交的标题 </Product>
<Product SSN='price'>55 </Product>
<Product SSN='stock'>32 </Product>
</root>
version 2.1.02
1.解决URL无后缀情况无法加载静态资源,解决无法渲染CSS文件。
version 2.1.03
1.移除com.j2mvc.StringUtils.getUtf8()方法调用
更改为getCharset()
version 2.1.04
1.去除Servlet和Filter的全局变量销毁,只交给Listener处理。
version 2.1.05,2.1.06,2.1.07
1.完善POST提交的JSON数据
支持接收基础数据类型、任意对象类型、任意数组类型。
不支持接收参数为集合类型或Map类型,但可以定义为接收对象类型的元素。
version 2.1.05,2.1.06,2.1.07
1.修改连接池变量
version 2.1.09
增加上传功能,修改RequestMethod,ContentType设置方式
version 2.1.10,2.1.11
更改上传文件名格式为UUID格式,移除JSON映射类,更改接收多文件上传。
version 2.1.12
删除文件列有的空对象
version 2.1.13
增加配置文件目录/conf,加载上传功能配置/conf/upload.properties
version 2.1.18
拦截器也能获取ActionBean
version 2.1.20
添加上传文件只读权限
version 2.1.21
支持同时接收文件和文本数据
version 2.1.22
增加文件接收类型media
version 2.1.23
删除upload类printJson方法
The newest version!
package com.j2mvc.framework.dispatcher;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.j2mvc.framework.Session;
import com.j2mvc.framework.action.ActionBean;
import com.j2mvc.framework.action.ActionMatch;
import com.j2mvc.framework.interceptor.Interceptor;
import com.j2mvc.framework.mapping.RequestMethod;
/**
* 调配Servlet
*
* 2014-2-23 创建@杨朔
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 4351854781111498245L;
static final Logger log = Logger.getLogger(DispatcherServlet.class);
private static final String METHOD_GET = "GET";;
private static final String METHOD_POST = "POST";
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding(Session.encoding);
resp.setContentType("text/html;charset=" + Session.encoding);
resp.setCharacterEncoding(Session.encoding);
String method = req.getMethod();
String uri = req.getRequestURI();
String contextPath = req.getContextPath();
if ("/".equals(contextPath)) {
contextPath = "";
}
uri = !contextPath.equals("") && uri.startsWith(contextPath) ? uri.substring(contextPath.length(), uri.length())
: uri;
if (uri.endsWith("/")) {
uri = uri.substring(0, uri.lastIndexOf("/"));
} else {
uri = uri.substring(0, uri.lastIndexOf("."));
}
ActionBean bean = new ActionMatch(uri).getBean();
/** 执行拦截器 */
Interceptor dispatcherInterceptor = new Interceptor(req, resp,bean);
boolean success = dispatcherInterceptor.isSuccess();
if (success) {
if (bean != null) {
String requestMethod = bean.getRequestMethod();
requestMethod = requestMethod!=null?requestMethod:"";
if (method.equals(METHOD_GET) &&
(requestMethod.equalsIgnoreCase(RequestMethod.GET)||
requestMethod.equals(""))) {
doAction(req, resp,bean);
} else if (method.equals(METHOD_POST) &&
(requestMethod.equalsIgnoreCase(RequestMethod.POST)||
requestMethod.equals(""))) {
doAction(req, resp,bean);
} else {
log.warn("服务器限制了请求模式,客户端请求方式是GET或POST,与服务器requestMothod不一致.");
super.service(req, resp);
}
}else{
String queryString = req.getQueryString();
if(Session.uriLog)
log.warn("正在访问>>" + uri +(queryString!=null&&!queryString.equals("")?"?"+queryString:"")+ ",未找到映射."
+ "如果配置uri为正则表达式,请检是否正确,"
+ "如果配置正确,请检查uri是否正确,例如是否漏了工程路径.");
super.service(req, resp);
}
}
}
/**
* 执行Actions
*
* @param request
* @param response
* @param bean
*/
public void doAction(HttpServletRequest request, HttpServletResponse response,ActionBean bean)
throws IOException, ServletException {
/** 执行Action */
new DispatcherForward(request, response, bean);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy