2
2
3
3
import org .slf4j .Logger ;
4
4
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 .*;
9
6
import simple .xfj .framework .bootstarp .HelperInitiler ;
10
7
import simple .xfj .framework .helper .BeanHelper ;
11
- import simple .xfj .framework .helper .ClassHelper ;
12
8
import simple .xfj .framework .helper .ConfigHelper ;
13
9
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 ;
14
13
import simple .xfj .framework .util .StreamUtil ;
15
14
16
15
import javax .servlet .*;
19
18
import javax .servlet .http .HttpServletRequest ;
20
19
import javax .servlet .http .HttpServletResponse ;
21
20
import java .io .IOException ;
21
+ import java .io .PrintWriter ;
22
22
import java .util .Enumeration ;
23
23
import java .util .HashMap ;
24
24
import java .util .Map ;
25
- import java .util .Set ;
26
25
27
26
/**
28
27
* Created by asus on 2017/4/18.
@@ -55,8 +54,14 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
55
54
Class <?> handlerClass = handler .getControllerClass ();
56
55
Object handlerBean = BeanHelper .getClassBean (handlerClass );
57
56
//创建param
58
- System .out .println ("yes" );
59
57
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
+ }
60
65
}
61
66
}
62
67
@@ -71,8 +76,17 @@ private Param getParam(HttpServletRequest req){
71
76
}
72
77
//form表单中的参数收集
73
78
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
+ }
76
90
} catch (IOException e ) {
77
91
LOGGER .error ("read param from Stream failure" );
78
92
throw new RuntimeException (e );
@@ -81,4 +95,37 @@ private Param getParam(HttpServletRequest req){
81
95
}
82
96
83
97
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
+ }
84
131
}
0 commit comments