forked from dixingxing0/halo1.1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
215 lines (198 loc) · 8.1 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# java的轻量级web框架 与分布式数据访问框架
## 框架基础环境:Servlet Spring3 jee5以上版本。 本框架的目的是为了简单的创建并使用web开发以及分布式数据库访问,让开人人员脱离struts2繁琐的配置以及冗余的annotation代码。 你只需要进行简单的配置,即可使用该框架进行web开发.
=======================================================
web 使用说明:
1,导入项目依赖jar文件
2,配置web.xml,示例:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- spring加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 字符编码过滤器加载 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- 框架filter 0 -->
<filter>
<filter-name>HaloWrapperFilter</filter-name>
<filter-class>halo.web.action.HaloWrapperFilter</filter-class>
</filter>
<!-- 框架filter 1 -->
<filter>
<filter-name>ActionFilter</filter-name>
<filter-class>halo.web.action.ActionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 包装request response,必须放在其他过滤器之前,除了字符编码过滤器 -->
<filter-mapping>
<filter-name>HaloWrapperFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- action请求过滤器,必须放在所有过滤器之后 -->
<filter-mapping>
<filter-name>ActionFilter</filter-name>
<url-pattern>*.do</url-pattern>
<!-- 目前测试REQUEST FORWARD 没有问题 -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
3.在spring配置文件中写入:
<!-- 在spring的配置文件中添加 -->
<context:annotation-config />
<!-- 通过annotation 扫描的根目录 -->
<context:component-scan base-package="demo.haloweb.dev3g.web">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
</context:component-scan>
<!-- must config -->
<bean id="haloUtil" class="halo.util.HaloUtil" />
<bean class="halo.web.action.ExceptionConfig">
<property name="exceptionMap">
<map>
<entry key="java.lang.Exception" value="/web/error.jsp">
</entry>
</map>
</property>
</bean>
<bean id="webCnf" class="halo.web.util.WebCnf">
<!-- 设置文件上传的临时目录 -->
<property name="uploadFileTempPath" value="/Users/fire9/temp/" />
<!-- 是否需要进行字符编码转换 -->
<property name="needCharsetEncode" value="true" />
<!-- 原编码 -->
<property name="sourceCharset" value="iso-8859-1" />
<!-- 目标编码 -->
<property name="targetCharset" value="utf-8" />
<!-- 强制url进行上传文件检查,不通过配置的url不能接收文件上传 -->
<property name="mustCheckUpload" value="true" />
<!-- 接收文件检查的url 格式:/actionname/method:[file size](单位为M) -->
<property name="fileUploadCheckUriCnfList">
<list>
<value>/hello/upload:20</value>
</list>
</property>
</bean>
4,在demo.haloweb.dev3g.web目录中写一个Action代码
package demo.haloweb.dev3g.web;
import halo.util.FileUtil;
import halo.web.action.HkRequest;
import halo.web.action.HkResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.Cookie;
import org.springframework.stereotype.Component;
import demo.haloweb.dev3g.model.User;
/**
* url运行方式:http://localhost:8080/webapp/hello/{methodname}.do
*
* @author akwei
*/
@Component("/hello")//配置namespace对应
public class HelloAction {
/**
* http://localhost:8080/heloweb/hello/say.do <br>
* 接收http请求参数
*
* @param req
* @param resp
* @return
*/
public String say(HkRequest req, HkResponse resp) {
String name = "测试中文" + req.getInt("name");//获取int类型参数
String key = "key" + req.getStringRow("key", "");//获取单行参数
req.setAttribute("name", name);
req.setEncodeAttribute("name", name);//setAttribute("enc_name",UrlEncode(name));
req.setAttribute("key", key);
req.setSessionValue("name_key", name + key);
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
req.setAttribute("list", list);
req.setAttribute("selectedValue", 5);
User user = new User();
req.buildBean(user);//对与user对象属性匹配的参数进行对象赋值,为了简化request.getParameter的过程
return "/web/hello_say.jsp";//forward的页面
}
/**
* 链接重定向 response.redirect(String);
*
* @param req
* @param resp
* @return
*/
public String say0(HkRequest req, HkResponse resp) {
return "r:/hello/say.do?name=233&key=hello";//redirect的地址
}
/**
* 文件上传 http://localhost:8080/heloweb/hello/upload.do <br>
*
* @param req
* @param resp
* @return
*/
public String upload(HkRequest req, HkResponse resp) throws Exception {
if (req.isUploadExceedSize()) {
resp.sendHtml("图片文件过大");
return null;
}
File file = req.getFile("f");
if (file == null) {
resp.sendHtml("没有文件上传");
return null;
}
FileUtil.copyFile(file, "/Users/fire9/mydev/temp", "aa");
return "/web/upload_success.jsp";
}
/**
* 设置cookie
*
* @param req
* @param resp
* @return
*/
public String setcookie(HkRequest req, HkResponse resp) throws Exception {
Cookie cookie = new Cookie("cookie", "123");
cookie.setPath("/");
cookie.setMaxAge(-1);
resp.addCookie(cookie);
resp.sendHtml("set cookie ok");
return null;
}
/**
* 获取cookie
*
* @param req
* @param resp
* @return
*/
public String getcookie(HkRequest req, HkResponse resp) throws Exception {
Cookie cookie = req.getCookie("cookie");
String value = cookie.getValue();
req.setAttribute("value", value);
return "/web/cookie_get.jsp";
}
}
5,运行tomcat,然后在地址栏输入 http://localhost:8080/webapp/hello/say.do 可以访问say这个方法
到此为基本的mvc运行,里面的Hkrequest HkResponse是HttpServletRequest HttpServletResponse的子类,可以直接使用