开发者学堂课程【Java Web开发系列课程 - Struts2框架入门:自定义框架_解析请求】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/537/detail/7330
自定义框架_解析请求
自定义框架解析-解析请求
将以下解析完,就会在过滤器里取有。将用户提交的请求,映射到对应的action型(
for(Element element :actions ){
)
上。
将过滤器取名为 CoreFilter。过滤器是用来实现 filter 接口的。
Document document = new SAXReader() .read(is);
Element root = document. getRootElement();
/
/
处理
action
节点
List<Element> actions=root. elements();
for(Element element :actions ){
Action action = new Action();
//
获取
action
的属性值
action. setName(element . attributeValue( "name"));
action. setClasses (element. attributeValue("class"));
String method=element. attributeValue(" method") ;
if (method!=null)
action. setMethod (method);l
/
/
处理
Actіon
中的结果集
List<Element> results = element . elements();
for(Element e:results){
过滤器中显示:
java.util.List<Element>
An ordered colection (also known as a sequence). The user of this interface has precise control over
where in the list each element is inserted. The user can access elements by their integer index
(position in the list), and search for elements in the list.
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of
elements e1 and e2 such that e1.equals (e2), and they typically allow multiple null elements if they
allow null elements at all. It is not inconceivable that someone might wish to implement a list that
prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we
expect this usage to be rare.
……
过滤器是用来实现 filter 接口的。
package cn. sxt. filter;
import javax.servlet.Filter;
public class CoreFilter implements Filterk{
public void destroy() {
/
/TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException{
// TODO Auto-generated method stub
}
public void init(FilterConfig arge) throws ServletException (//T//TODO Auto- generated method stub
}
import javax. servlet . FilterConfig;
import javax. servlet . ServletException;
import javax. servlet . ServletRequest ;
import javax. servlet. ServletResponse;
public class CoreFilter implements Filter{
public void destroy() {
}
public void init() throws ServletException{
}
public void doFilter(ServletRequest req, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
//TODO Auto- generatedmethod stub
将 req 改成 rq,arg1改成 rp,arg2改成 chain。
import javax. servlet . FilterConfig;
import javax. servlet . ServletException;
import javax. servlet . ServletRequest ;
import javax. servlet. ServletResponse;
12 public class CoreFilter implements Filter{
public void destroy() {
}
public void init() throws ServletException{
}
public void doFilter(ServletRequest rq, ServletResponse rp,
FilterChain chain) throws IOException, ServletException {
//TODO Auto- generated method stub
}
public void init(FilterConfig arge) throws ServletException {
//TODO Auto- generated method stub
}
}
将 public void init(FilterConfig arge) throws ServletException {
写入,去掉public void init() throws ServletException{
,config 未使用过。将 ServletRequest 、 ServletResponse 全部转换。
import javax. servlet . FilterConfig;
import javax. servlet . ServletException;
import javax. servlet . ServletRequest ;
import javax. servlet. ServletResponse;
public class CoreFilter implements Filter{
public void destroy() {
}
}
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest rq, ServletResponse rp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)rq;
(强转)
HttpServletResponse resp = (HttpServletResponse)rp;
//TODO Auto- generated method stub
/
/
第一件事:将请求映射到 a
ction
上(分别用单独的方法处理,上面就变成第二种方法)
r
eqToAction(req);
(注意:在实验过程中要持续测试,去了解)
/
/
创建a
ction
对象
/
/
将用户提交的数据设置到a
ction
的属性上
/
/
执行a
ction
的方法
/
/
处理结果
}
用某个类来处理。将请求映射到 action 型上,返回 action 型对象。返回空,调方法。getRequestURI0 : String
-
HttpServletRequest
和
getRequestURL0: StringBuffer
-
HtpServletRequest
的区别是 getRequestURI0 : String HttpServletRequest
可以接收后面的
接着验证是不是 action 结尾。固定结尾,或自定义扩展名,甚至没有。也可以验证只要有点就去掉。如果不是. action 结尾的,直接请求下一部的事情。
将请求映射到 action 上
Private Action reqToAction(HttpServletRequest req ){
S
tring path=req.getRequestURI();
/
/
只处理以a
ction
结尾的请求
i
f (path.endswith(“.action”))
Return null;
/
/
获得请求名
String reqName=path. substring (path.lastIndexO
f
(“/”)+1, path.lastIndexOf(“.”))
/
/
程序启动后解析配置文件,解析不只是解析一次,开始就要解析。在
public void init(FilterConfig config) throws ServletException {
和
}
public void doFilter(ServletRequest rq, ServletResponse rp,
间,加入
ActionMapper. parser();
,
启动值后解析文件,回车后,
(
public void init(FilterConfig config)I throws ServletException {
try {
//
程序启动后解析配置文件,只会配置一次。i
nter
只会做一次。
ActionMapper .parser();
} catch (DocumentException e) {
)
return ActionMapper.actionMap.get(reqName);
(根据请求名取)
}
r
eqToAction(req);
将请求名获取到这,变成 A
ction
targetAction=
r
eqToAction(req)
;在进行测试。观察 Acton 是否对的。A
ction targetAction=
r
eqToAction(req);
System.out.println(targetAction.getName()+”---class”+targetAction.getClasses());
都进行测试,
发布并启动2
0myframework
。
请求l
ocallhost:8080/framework/hello.action
T
his is my JSP page
HTTP Status 404- /framework/hello.action
type Status report
message framework/hello.action
description The requested resource is not available.
Apache Tomcat/7.0.62
将过滤器配置。写代码先将思路捋出,接着测试好每一步骤的结果。
<?xml version= "1.
Ѳ
"
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">
<display-name></display-name>
<filter>
<filter-name>corefilter</filter-name>
<filter-class>cn. sxt.filter. CoreFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>corefilter</filter-name>
<url-pattern>*.action</ url-pattern >
(不能限制用户填的东西)
<filter-mapping>
<
welcome-file-list>
<
welcome-file>index.jsp</welcome-file>
C
onsole
信息:Starting ProtocolHandler ["ajp-apr- 8009" ]
2015-8
-
20 11:17 :47 org. apache. catalina. startup.Catalina start
信息: Server startup in 4028 ms
hello---classqn. sxt. action. HelloAction
Hello 被请出,classcn 等于地方。说明已完成获取 filter。