Skip to content

Commit 5ad4659

Browse files
author
monsoon
committed
finish dispatcherServlet
1 parent 3c22bbe commit 5ad4659

File tree

5 files changed

+147
-15
lines changed

5 files changed

+147
-15
lines changed

src/main/java/simple/xfj/framework/DispatcherServlet.java

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5-
import simple.xfj.framework.annotation.Action;
6-
import simple.xfj.framework.bean.Handler;
7-
import simple.xfj.framework.bean.Param;
8-
import simple.xfj.framework.bean.Request;
5+
import simple.xfj.framework.bean.*;
96
import simple.xfj.framework.bootstarp.HelperInitiler;
107
import simple.xfj.framework.helper.BeanHelper;
11-
import simple.xfj.framework.helper.ClassHelper;
128
import simple.xfj.framework.helper.ConfigHelper;
139
import simple.xfj.framework.helper.ControllerHelper;
10+
import simple.xfj.framework.util.DEcodeUtil;
11+
import simple.xfj.framework.util.JsonUtil;
12+
import simple.xfj.framework.util.ReflectionUtil;
1413
import simple.xfj.framework.util.StreamUtil;
1514

1615
import javax.servlet.*;
@@ -19,10 +18,10 @@
1918
import javax.servlet.http.HttpServletRequest;
2019
import javax.servlet.http.HttpServletResponse;
2120
import java.io.IOException;
21+
import java.io.PrintWriter;
2222
import java.util.Enumeration;
2323
import java.util.HashMap;
2424
import java.util.Map;
25-
import java.util.Set;
2625

2726
/**
2827
* Created by asus on 2017/4/18.
@@ -55,8 +54,14 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
5554
Class<?> handlerClass = handler.getControllerClass();
5655
Object handlerBean = BeanHelper.getClassBean(handlerClass);
5756
//创建param
58-
System.out.println("yes");
5957
Param param = getParam(req);
58+
System.out.println(handler.getActionMethod().getName());
59+
Object res = ReflectionUtil.methodInvoke(handler.getActionMethod(), handlerBean, param);
60+
try {
61+
doResult(req,resp,res);
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
}
6065
}
6166
}
6267

@@ -71,8 +76,17 @@ private Param getParam(HttpServletRequest req){
7176
}
7277
//form表单中的参数收集
7378
try {
74-
String StringParam = StreamUtil.getStringFromStream(req.getInputStream());
75-
System.out.println(StringParam);
79+
String StringParam = DEcodeUtil.Decode(StreamUtil.getStringFromStream(req.getInputStream()));
80+
if(StringParam.length() == 0)
81+
return new Param(paramMap);
82+
String[] nameValue = StringParam.split("&");
83+
if(nameValue != null && nameValue.length > 0){
84+
for(int i = 0; i < nameValue.length;i++){
85+
System.out.println(nameValue[i]);
86+
String[] pair = nameValue[i].split("=");
87+
paramMap.put(pair[0],pair[1]);
88+
}
89+
}
7690
} catch (IOException e) {
7791
LOGGER.error("read param from Stream failure");
7892
throw new RuntimeException(e);
@@ -81,4 +95,37 @@ private Param getParam(HttpServletRequest req){
8195
}
8296

8397

98+
private void doResult(HttpServletRequest req,HttpServletResponse pon,Object res) throws Exception{
99+
if(res == null){
100+
req.getRequestDispatcher(ConfigHelper.getAPPJspPath() + "error.jsp").forward(req,pon);
101+
}
102+
if(res instanceof View){
103+
View view = (View) res;
104+
String path = view.getPath();
105+
if(path != null && path.length() > 0){
106+
if(path.startsWith("/")){
107+
pon.sendRedirect(req.getContextPath() + path);
108+
}else{
109+
Map<String, Object> model = view.getModel();
110+
for(Map.Entry<String,Object> ele : model.entrySet()){
111+
req.setAttribute(ele.getKey(),ele.getValue());
112+
}
113+
req.getRequestDispatcher(ConfigHelper.getAPPJspPath()).forward(req,pon);
114+
}
115+
}
116+
}else if(res instanceof Data){
117+
//返回json数据
118+
Data data = (Data) res;
119+
Object model = data.getModel();
120+
if(null != model){
121+
pon.setContentType("application/json");
122+
pon.setCharacterEncoding("UTF-8");
123+
PrintWriter writer = pon.getWriter();
124+
String json = JsonUtil.ObjToStr(model);
125+
writer.write(json);
126+
writer.flush();
127+
writer.close();
128+
}
129+
}
130+
}
84131
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package simple.xfj.framework.util;
2+
3+
import com.sun.org.apache.xml.internal.utils.StringToStringTableVector;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URLDecoder;
9+
import java.net.URLEncoder;
10+
11+
/**
12+
* Created by asus on 2017/4/19.
13+
*/
14+
public class DEcodeUtil {
15+
16+
private static final Logger LOGGER = LoggerFactory.getLogger(DEcodeUtil.class);
17+
18+
public static final String Decode(String str){
19+
String res = null;
20+
try {
21+
/* byte[] bytes = str.getBytes("ISO8859-1");
22+
res = new String(bytes, "GBK");*/
23+
res = URLDecoder.decode(str,"UTF-8");
24+
} catch (UnsupportedEncodingException e) {
25+
LOGGER.error("decode String error");
26+
throw new RuntimeException(e);
27+
}
28+
return res;
29+
}
30+
31+
public static final String Encode(String str){
32+
try {
33+
str = URLEncoder.encode(str,"ISO8859-1");
34+
} catch (UnsupportedEncodingException e) {
35+
LOGGER.error("encode String error");
36+
throw new RuntimeException(e);
37+
}
38+
return str;
39+
}
40+
41+
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package simple.xfj.framework.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.io.IOException;
9+
10+
/**
11+
* Created by asus on 2017/4/19.
12+
*/
13+
public class JsonUtil {
14+
15+
private static final ObjectMapper MAPPER = new ObjectMapper();
16+
17+
private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
18+
19+
public static final String ObjToStr(Object object){
20+
String res = null;
21+
try {
22+
res = MAPPER.writeValueAsString(object);
23+
} catch (JsonProcessingException e) {
24+
LOGGER.error("transform object to json string failure");
25+
throw new RuntimeException(e);
26+
}
27+
return res;
28+
}
29+
30+
public static final <T> T strToObj(String str,Class<T> clazz){
31+
T t = null;
32+
try {
33+
t = MAPPER.readValue(str,clazz);
34+
} catch (IOException e) {
35+
LOGGER.error("parse json to obj string failure");
36+
throw new RuntimeException(e);
37+
}
38+
return t;
39+
}
40+
41+
}

src/main/java/simple/xfj/framework/util/ReflectionUtil.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public static Object methodInvoke(Method method,Object obj,Object... args){
3030
Object result = null;
3131
if(!method.isAccessible()){
3232
method.setAccessible(true);
33-
try {
34-
result = method.invoke(obj,args);
35-
} catch (Exception e) {
36-
LOGGER.error("can not invoke method:" + method.getName());
37-
throw new RuntimeException("invoke method failure:" + method.getName());
38-
}
33+
}
34+
try {
35+
result = method.invoke(obj,args);
36+
} catch (Exception e) {
37+
LOGGER.error("can not invoke method:" + method.getName());
38+
throw new RuntimeException("invoke method failure:" + method.getName());
3939
}
4040
return result;
4141
}

src/main/java/simple/xfj/framework/util/StreamUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static String getStringFromStream(InputStream in){
2121
while ((line = reader.readLine())!= null){
2222
sb.append(line);
2323
}
24+
reader.close();
25+
in.close();
2426
}catch (Exception e){
2527
LOGGER.error("read String from Stream failure");
2628
throw new RuntimeException(e);

0 commit comments

Comments
 (0)