第一章 自定义MVC框架
第一章? 自定義MVC框架
1.1 MVC模式設(shè)計(jì)
?? ?組成;Model:模型,用于數(shù)據(jù)和業(yè)務(wù)的處理
?? ??? ?? View :視圖,用于數(shù)據(jù)的顯示
?? ??? ?? Controller:控制器,用于進(jìn)行流程控制
?? ?特點(diǎn):1.一個(gè)模型可以對(duì)應(yīng)多個(gè)視圖
?? ??? ?? 2.顯示與邏輯控制的分離
?? ??? ?? 3.分層控制,減低了代碼間的耦合
1.2 DTD驗(yàn)證XML文檔
?? ??? ?聲明:<!DOCTYPE 根元素 [定義內(nèi)容]>
?? ??? ??? ?例:<!DOCTYPE poem [
?? ??? ??? ??? ??? ?<!ELEMENT poem (author,title,content)>
?? ??? ??? ??? ??? ?<!ELEMENT author (#PCDATA)>
?? ??? ??? ??? ??? ?<!ELEMENT title (#PCDATA)>
?? ??? ??? ??? ??? ?<!ELEMENT content (#PCDATA)>
?? ??? ??? ??? ?]>
?? ??? ??? ??? ?<peom>
?? ??? ??? ??? ??? ?<author>王維</author>
?? ??? ??? ??? ??? ?<title>鹿柴</title>
?? ??? ??? ??? ??? ?<content>空山不見人,但聞人語聲。返景入深林,復(fù)照青苔上</content>
?? ??? ??? ??? ?</peom>
?? ??? ?使用外部DTD驗(yàn)證XML
?? ??? ??? ??? ?myStruts.dtd:
?? ??? ??? ??? ?<!ELEMENT poems (poem*)>
?? ??? ??? ??? ??? ?<!ELEMENT poem (title,author,year,content)>
?? ??? ??? ??? ??? ?<!ELEMENT title (#PCDATA)>
?? ??? ??? ??? ??? ?<!ELEMENT author (#PCDATA)>
?? ??? ??? ??? ??? ?<!ELEMENT year (#PCDATA)>
?? ??? ??? ??? ??? ?<!ELEMENT content (#PCDATA)>
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?myMvc.xml:
?? ??? ??? ??? ?<!DOCTYPE poems SYSTEM "myStruts.dtd">
?? ??? ??? ??? ?<poems>
?? ??? ??? ??? ??? ?<poem>
?? ??? ??? ??? ??? ??? ?<title>春曉</title>
?? ??? ??? ??? ??? ??? ?<author>孟浩然</author>
?? ??? ??? ??? ??? ??? ?<content>春眠不覺曉......</content>
?? ??? ??? ??? ??? ?</poem>
?? ??? ??? ??? ?</poems>
?? ??? ?DTD元素
?? ??? ??? ?<!ELEMENT name CONTENT>
?? ??? ??? ?ELEMENT:關(guān)鍵字
?? ??? ??? ?name:元素名稱
?? ??? ??? ?CONTENT:元素類型
?? ??? ??? ??? ??? ?? #PCDATA:任何字符數(shù)據(jù),不能在其中包含任何子元素
?? ??? ??? ??? ??? ?? 純?cè)仡愋?#xff1a;只包含子元素,且這些子元素外沒有文本
?? ??? ??? ?符號(hào):():表示分成幾組
?? ??? ??? ??? ??? | :必須選其一
?? ??? ??? ??? ??? ,:按順序出現(xiàn)
?? ??? ??? ??? ??? * :可以出現(xiàn)0到多次
?? ??? ??? ??? ??? ?:可出現(xiàn)也可不出現(xiàn),出現(xiàn)且只有一次
?? ??? ??? ??? ??? + :必須出現(xiàn),且可以出現(xiàn)多次
?? ??? ?DTD屬性
?? ??? ??? ?<!ATTLIST 元素名稱? 屬性名稱? 屬性類型? 屬性默認(rèn)值>
?? ??? ??? ?屬性類型描述:
?? ??? ??? ??? ??? ??? ?CDATA:字符數(shù)據(jù)
?? ??? ??? ??? ??? ??? ?ID:唯一ID
?? ??? ??? ??? ??? ??? ?IDREFS:另一個(gè)ID
?? ??? ??? ??? ??? ??? ?ENTITY:實(shí)體
?? ??? ??? ??? ??? ??? ?ENTITIES:實(shí)體列表
?? ??? ??? ?屬性值描述:
?? ??? ??? ??? ??? ??? ?#REQUIRED:屬性值是必須的
?? ??? ??? ??? ??? ??? ?#IMPLIED:屬性值不是必須的
?? ??? ??? ??? ??? ??? ?#FIXED:屬性值是固定的
?? ??? ??? ??? ??? ??? ?例:<!DOCTYPE mystruts[
?? ??? ??? ??? ??? ??? ??? ?<!ELEMENT mystruts (actions)>
?? ??? ??? ??? ??? ??? ??? ?<!ELEMENT actions (action*)>
?? ??? ??? ??? ??? ??? ??? ?<!ELEMENT action (result*)>
?? ??? ??? ??? ??? ??? ??? ?<!ATTLIST auction
?? ??? ??? ??? ??? ??? ??? ??? ?name CDATA #REQUIRED
?? ??? ??? ??? ??? ??? ??? ??? ?CLASS CDATA #REQUIRED>
?? ??? ??? ??? ??? ??? ??? ?<!ATTLIST result
?? ??? ??? ??? ??? ??? ??? ??? ?name CDATA #IMPLIED
?? ??? ??? ??? ??? ??? ??? ??? ?redirect (true|false) "false">
?? ??? ??? ??? ??? ??? ?]>
?? ??? ??? ??? ??? ??? ?<mystruts>
?? ??? ??? ??? ??? ??? ??? ?<actions>
?? ??? ??? ??? ??? ??? ??? ??? ?<action name="register" class="cn.jbit.action.RegisterAction">
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<result name="success">page/login.jsp</result>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<result name="input">page/register.jsp</result>
?? ??? ??? ??? ??? ??? ??? ??? ?</action>
?? ??? ??? ??? ??? ??? ??? ??? ?<action name="login" class="cn.jbit.mvc.LoginAction">
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<result name="success">page/guanli.jsp</result>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<result name="input">page/login.jsp</result>
?? ??? ??? ??? ??? ??? ??? ??? ?</action>
?? ??? ??? ??? ??? ??? ??? ?</actions>
?? ??? ??? ??? ??? ??? ?</mystruts>
1.3 XML文檔解析
?? ??? ?獲取root節(jié)點(diǎn) ?? ?ELement root
?? ??? ?1、獲取節(jié)點(diǎn)名稱? root.getName()
?? ??? ?2、獲取節(jié)點(diǎn)屬性? root.attribute("dname")
?? ??? ?3、獲取節(jié)點(diǎn)中的text? root.getText()
?? ??? ?4、獲取子節(jié)點(diǎn)
?? ??? ?
?? ??? ?使用DOM4J操作XML數(shù)據(jù)(如:dom4j-1.6.1.jar)
?? ??? ?例:student.xml:
?? ??? ??? ??? ?<?xml version="1.0" encoding="gb2312"?>
?? ??? ??? ??? ?<students>
?? ??? ??? ??? ??? ?<student age="31"><!-- 如果沒有固定的age 默認(rèn)為20? -->
?? ??? ??? ??? ??? ??? ?<name>崔衛(wèi)兵</name>
?? ??? ??? ??? ??? ??? ?<college>pc學(xué)院</college>
?? ??? ??? ??? ??? ??? ?<telephone>6234666</telephone>
?? ??? ??? ??? ??? ??? ?<notes>男,1985年出生,碩士</notes>
?? ??? ??? ??? ??? ?</student>
?? ??? ??? ??? ??? ?<student>
?? ??? ??? ??? ??? ??? ?<name>張洪澤</name>
?? ??? ??? ??? ??? ??? ?<!-- 如果沒有固定的leader 默認(rèn)為leader? -->
?? ??? ??? ??? ??? ??? ?<college leader="Leader">pc學(xué)院</college>
?? ??? ??? ??? ??? ??? ?<telephone>6238888</telephone>
?? ??? ??? ??? ??? ??? ?<notes>男,1987年出生,碩士</notes>
?? ??? ??? ??? ??? ?</student>
?? ??? ??? ??? ?</students>
?? ??? ??? ?Dom4jReadExmple.java:
?? ??? ??? ??? ?public class Dom4jReadExmple {
?? ??? ??? ??? ??? ?public void iterateWholeXML(String filename,HashMap<String,String> hm){
?? ??? ??? ??? ??? ??? ?SAXReader saxReader=new SAXReader();
?? ??? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ??? ?Document document=saxReader.read(new File(filename));
?? ??? ??? ??? ??? ??? ??? ?Element root=document.getRootElement();
?? ??? ??? ??? ??? ??? ??? ?int num=-1;//記錄學(xué)生編號(hào)的變量
?? ??? ??? ??? ??? ??? ??? ?//遍歷根元素(students)的所有子節(jié)點(diǎn)(student)
?? ??? ??? ??? ??? ??? ??? ?for(Iterator iter=root.elementIterator();iter.hasNext();){
?? ??? ??? ??? ??? ??? ??? ??? ?Element element=(Element) iter.next();
?? ??? ??? ??? ??? ??? ??? ??? ?num++;
?? ??? ??? ??? ??? ??? ??? ??? ?//獲取person節(jié)點(diǎn)的age屬性
?? ??? ??? ??? ??? ??? ??? ??? ?Attribute ageAttribute=element.attribute("age");
?? ??? ??? ??? ??? ??? ??? ??? ?if(ageAttribute!=null){
?? ??? ??? ??? ??? ??? ??? ??? ??? ?String age=ageAttribute.getValue();
?? ??? ??? ??? ??? ??? ??? ??? ??? ?if(age!=null && !age.equals("")){
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(element.getName()+"-"+ageAttribute.getName()+num, age);
?? ??? ??? ??? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(element.getName()+"-"+ageAttribute.getName()+num, "20");
?? ??? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(element.getName()+"-age"+num, "20");
?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ?//遍歷student節(jié)點(diǎn)下的所有子節(jié)點(diǎn)(name,college,telephone,notes)
?? ??? ??? ??? ??? ??? ??? ??? ?for(Iterator iterInner=element.elementIterator();iterInner.hasNext();){
?? ??? ??? ??? ??? ??? ??? ??? ??? ?Element elementInner=(Element) iterInner.next();
?? ??? ??? ??? ??? ??? ??? ??? ??? ?if(elementInner.getName().equals("college")){
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(elementInner.getName()+num,elementInner.getText());
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?//獲取college節(jié)點(diǎn)的leader屬性
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?Attribute leaderAttr=elementInner.attribute("leader");
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?if(leaderAttr!=null){
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?String leader=leaderAttr.getValue();
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?if(leader!=null && !leader.equals("")){
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(elementInner.getName()+"-"+leaderAttr.getName()+num,leader);
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(elementInner.getName()+"-"+leaderAttr.getName()+num,"leader");
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(elementInner.getName()+"-leader"+num, "leader");
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?hm.put(elementInner.getName()+num, elementInner.getText());
?? ??? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?} catch (DocumentException e) {
?? ??? ??? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?TestDom4jReadExmple.java:
?? ??? ??? ??? ??? ?public class TestDom4jReadExmple {
?? ??? ??? ??? ??? ?public static void main(String[] args) {
?? ??? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ??? ?//獲取解析后的解析信息
?? ??? ??? ??? ??? ??? ??? ?HashMap<String, String> hashMap;
?? ??? ??? ??? ??? ??? ??? ?Dom4jReadExmple dre=new Dom4jReadExmple();
?? ??? ??? ??? ??? ??? ??? ?//遍歷xml文件
?? ??? ??? ??? ??? ??? ??? ?hashMap=new HashMap<String, String>();
?? ??? ??? ??? ??? ??? ??? ?String n=System.getProperty("user.dir");
?? ??? ??? ??? ??? ??? ??? ?//獲取xml文件
?? ??? ??? ??? ??? ??? ??? ?dre.iterateWholeXML(n+"\\src\\cn\\jbit\\action\\Student.xml", hashMap);
?? ??? ??? ??? ??? ??? ??? ?for (int i = 0; i < hashMap.size(); i++) {
?? ??? ??? ??? ??? ??? ??? ??? ?int j=i/6;
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.print(hashMap.get("name"+j)+"\t");
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.print(hashMap.get("student-age"+j)+"\t");
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.print(hashMap.get("college"+j)+"\t");
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.print(hashMap.get("college-leader"+j)+"\t");
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.print(hashMap.get("telephone"+j)+"\t");
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.println(hashMap.get("notes"+j)+"\t");
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ?輸出結(jié)果:崔衛(wèi)兵?? ?31?? ?pc學(xué)院?? ?leader?? ?6234666?? ?男,1985年出生,碩士?? ?
?? ??? ??? ??? ?? 張洪澤?? ?20?? ?pc學(xué)院?? ?Leader?? ?6238888?? ?男,1987年出生,碩士
?? ??? ??? ??? ?
?? ??? ?關(guān)鍵操作如下:
?? ??? ??? ?1、Document對(duì)象相關(guān)
?? ??? ??? ??? ?SAXReader saxReader=new SAXReader();
?? ??? ??? ??? ?Document document=saxReader.read(new File("index.xml"));
?? ??? ??? ?2、節(jié)點(diǎn)相關(guān)?? ?
?? ??? ??? ??? ?1、獲取文檔的根節(jié)點(diǎn)
?? ??? ??? ??? ??? ?Element root=document.getRootElement();
?? ??? ??? ??? ?2、獲取某節(jié)點(diǎn)的單個(gè)子節(jié)點(diǎn)
?? ??? ??? ??? ??? ?Element meberElm=root.element("Member");
?? ??? ??? ??? ?3、取得節(jié)點(diǎn)的文字
?? ??? ??? ??? ??? ?String text=MemberElm.getText();
?? ??? ??? ??? ?4、取得某節(jié)點(diǎn)下名為:Member的所有子節(jié)點(diǎn)并進(jìn)行遍歷
?? ??? ??? ??? ??? ?List nodes=rootElm.elements("Member");
?? ??? ??? ??? ?5、對(duì)某節(jié)點(diǎn)下的所有子節(jié)點(diǎn)進(jìn)行遍歷
?? ??? ??? ??? ??? ?for(Iterator iterInner=element.elementIterator();iterInner.hasNext();){
?? ??? ??? ??? ??? ??? ?Element elementInner=(Element) iterInner.next();
?? ??? ??? ??? ??? ??? ?//...
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?6、在某節(jié)點(diǎn)下添加子節(jié)點(diǎn)
?? ??? ??? ??? ??? ?Element ageElm=newMemberElm.addElement("age");
?? ??? ??? ??? ?7、設(shè)置節(jié)點(diǎn)文字
?? ??? ??? ??? ??? ?ageElm.setText("19");
?? ??? ??? ??? ?8、刪除某節(jié)點(diǎn)
?? ??? ??? ??? ??? ?parentElm.remove(childElm);//childElm是待刪除的節(jié)點(diǎn),parentElm是父節(jié)點(diǎn)
?? ??? ??? ??? ?9、添加一個(gè)CDATA節(jié)點(diǎn)
?? ??? ??? ??? ??? ?Element contentElm=infoElm.addElement(content);
?? ??? ??? ??? ??? ?contentElm.addCDATA(diary.getContent());
?? ??? ??? ??? ??? ?contentElm.getText();
?? ??? ??? ??? ??? ?contentElm.clearContent();
?? ??? ??? ?3、屬性相關(guān)
?? ??? ??? ??? ?1、取得某節(jié)點(diǎn)下的屬性
?? ??? ??? ??? ??? ?Element root=document.getRootElement();
?? ??? ??? ??? ??? ?Attribute ageAttribute=root.attribute("age");
?? ??? ??? ??? ?2、取得屬性的文字
?? ??? ??? ??? ??? ?String text=ageAttribute.getText();
?? ??? ??? ??? ?3、遍歷某節(jié)點(diǎn)的所有屬性
?? ??? ??? ??? ??? ?Element root=document.getRootElement();
?? ??? ??? ??? ??? ?for(Iterator it=root.elementIterator();it.hasNext();){
?? ??? ??? ??? ??? ??? ?Attribute attribute=(Attrinute) it.next();
?? ??? ??? ??? ??? ??? ?String text=ageAttribute.getText();
?? ??? ??? ??? ??? ??? ?System.out.print(text);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?4、設(shè)置某節(jié)點(diǎn)的屬性和文字
?? ??? ??? ??? ??? ?newMemberElm.addAttribute("name","sitinspring');
?? ??? ??? ??? ?5、設(shè)置屬性的文字
?? ??? ??? ??? ??? ?Attribute attribute=root.attribute("age");
?? ??? ??? ??? ??? ?attribute.setText("sitinspring");
1.4 反射機(jī)制
?? ??? ?反射的3個(gè)動(dòng)態(tài)性質(zhì)
?? ??? ??? ?1.運(yùn)行時(shí)伸出對(duì)象實(shí)例
?? ??? ??? ?2.運(yùn)行期間調(diào)用對(duì)象
?? ??? ??? ?3.運(yùn)行時(shí)更改對(duì)象
?? ??? ??? ?
?? ??? ?反射常用API
?? ??? ??? ?Class類:反射核心類
?? ??? ??? ?Field類:類的屬性
?? ??? ??? ?Method類:類的方法
?? ??? ??? ?Constructor類:類的構(gòu)造方法
?? ??? ??? ?
?? ??? ?使用反射的步驟
?? ??? ??? ?1、導(dǎo)入jar包
?? ??? ??? ?2、獲得需要操作的類的java.lang.CLass對(duì)象
?? ??? ??? ?3、調(diào)用Class的方法獲取Field、Method等對(duì)象
?? ??? ??? ?4、使用反射API進(jìn)行操作
?? ??? ??? ?
?? ??? ?反射的應(yīng)用
?? ??? ??? ?1、獲取Class對(duì)象
?? ??? ??? ??? ?例;
?? ??? ??? ??? ?Class clazz=Class.forName("java.lang.String");//正確
?? ??? ??? ??? ?Class clazz=Class.forName("String");//錯(cuò)誤
?? ??? ??? ?2、從Class對(duì)象獲取信息
?? ??? ??? ??? ?訪問類信息的常用方法
?? ??? ??? ??? ??? ?Constructor[] getConstructors()//返回所表示的類的public構(gòu)造方法
?? ??? ??? ??? ??? ?Constructor[] getDeclaredConstructors()//返回所表示的類的構(gòu)造方法
?? ??? ??? ??? ??? ?Method [] getMethods()//返回所表示的類的public方法
?? ??? ??? ??? ??? ?Method [] getDeclaredMethods()//返回所表示的類的全部方法
?? ??? ??? ??? ??? ?Field[] getFields()//返回所表示的類的public屬性
?? ??? ??? ??? ??? ?field[] getDeclaredFields()//返回所表示的類的全部屬性
?? ??? ??? ??? ??? ?Object get(Object obj)//得到引用類屬性值
?? ??? ??? ??? ??? ?void set(Object obj,Object val)//將Obj對(duì)象的屬性設(shè)置val值,針對(duì)引用類型
?? ??? ??? ??? ??? ?Object invoke(Object obj,Object args)//調(diào)用類的方法obj是執(zhí)行方法的對(duì)象,args是執(zhí)行方法時(shí)傳入的參數(shù)
?? ??? ??? ?3、創(chuàng)建對(duì)象
?? ??? ??? ??? ?newInstance():
?? ??? ??? ??? ?例:Object retBean=defaultCtor.newInstance();
1.5 構(gòu)建基于MVC模式的框架(Controller為MVC的核心)?? ??? ??? ??? ?
?? ??? ?1.5.1、Controller的設(shè)計(jì)
?? ??? ??? ?1、定義Action接口
?? ??? ??? ??? ?public interface Action {
?? ??? ??? ??? ??? ?public String execute(HttpServletRequest request,HttpServletResponse response)throws Exception;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?實(shí)現(xiàn)接口:
?? ??? ??? ??? ?public class LoginAction implements Action {
?? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ?public String execute(HttpServletRequest request,
?? ??? ??? ??? ??? ??? ??? ?HttpServletResponse response) throws Exception {
?? ??? ??? ??? ??? ??? ?String name=request.getParameter("name");
?? ??? ??? ??? ??? ??? ?String password=request.getParameter("password");
?? ??? ??? ??? ??? ??? ?//業(yè)務(wù)處理?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?UserBiz userBiz=new UserBizImpl();
?? ??? ??? ??? ??? ??? ?User user=userBiz.login(name,password);?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?//判斷是否登錄成功
?? ??? ??? ??? ??? ??? ?if(user==null){
?? ??? ??? ??? ??? ??? ??? ?request.setAttribute("message","用戶名或密碼錯(cuò)誤!");
?? ??? ??? ??? ??? ??? ??? ?return "/page/login.jsp";
?? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ?request.getSession().setAttribute("login", user);
?? ??? ??? ??? ??? ??? ??? ?return "/page/guanli.jsp";
?? ??? ??? ??? ??? ??? ?}?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?return null;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?2、實(shí)現(xiàn)Controller類
?? ??? ??? ??? ?public class ActionFilter implements Filter {
?? ??? ??? ??? ??? ?private FilterConfig config;
?? ??? ??? ??? ??? ?private ActionMappingManager mappingManager;
?? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ?public void destroy() {
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ?public void doFilter(ServletRequest request, ServletResponse response,
?? ??? ??? ??? ??? ??? ??? ?FilterChain chain) throws IOException, ServletException {
?? ??? ??? ??? ??? ??? ?//將請(qǐng)求裝換成HttpServletRequest
?? ??? ??? ??? ??? ??? ?HttpServletRequest hsr=(HttpServletRequest) request;
?? ??? ??? ??? ??? ??? ?HttpServletResponse hsp=(HttpServletResponse) response;
?? ??? ??? ??? ??? ??? ?//調(diào)用Action的execute方法
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ? Action action=this.getAction(hsr);
?? ??? ??? ??? ??? ??? ? String resultView=null;
?? ??? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ??? ?resultView=action.execute(hsr, hsp);
?? ??? ??? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?//頁面跳轉(zhuǎn)
?? ??? ??? ??? ??? ??? ?if(resultView!=null){
?? ??? ??? ??? ??? ??? ??? ?request.getRequestDispatcher(resultView).forward(request, response);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ?public void init(FilterConfig conf) throws ServletException {
?? ??? ??? ??? ??? ??? ?this.config=conf;
?? ??? ??? ??? ??? ??? ?String conStr=config.getInitParameter("config");
?? ??? ??? ??? ??? ??? ?//可包含多個(gè)配置文件
?? ??? ??? ??? ??? ??? ?String[] configFiles=null;
?? ??? ??? ??? ??? ??? ?if(conStr==null||conStr.isEmpty()){
?? ??? ??? ??? ??? ??? ??? ?configFiles=new String[]{"myMvc.xml"};
?? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ?//拆分配置文件名稱字符串
?? ??? ??? ??? ??? ??? ??? ?configFiles=conStr.split(",");
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?this.mappingManager=new ActionMappingManager(configFiles);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?private Action getAction(HttpServletRequest request) {
?? ??? ??? ??? ??? ??? ?//獲取請(qǐng)求的uri
?? ??? ??? ??? ??? ??? ?String uri = request.getRequestURI();
?? ??? ??? ??? ??? ??? ?//獲取上下文路徑
?? ??? ??? ??? ??? ??? ?String contextPath=request.getContextPath();
?? ??? ??? ??? ??? ??? ?//截取上下文路勁后面的部分
?? ??? ??? ??? ??? ??? ?String actionPath=uri.substring(contextPath.length());
?? ??? ??? ??? ??? ??? ?//獲取Action名稱
?? ??? ??? ??? ??? ??? ?String actionName=actionPath.substring(1,actionPath.lastIndexOf(".")).trim();
?? ??? ??? ??? ??? ??? ?Action action=null;
?? ??? ??? ??? ??? ??? ?//添加新功能時(shí)在這里添加
?? ??? ??? ??? ??? ??? ?if("login".equals(actionName)){
?? ??? ??? ??? ??? ??? ??? ?action=new LoginAction();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?return null;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ?web.xml配置:
?? ??? ??? ?<?xml version="1.0" encoding="UTF-8"?>
?? ??? ??? ?<web-app version="3.0"
?? ??? ??? ??? ?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_3_0.xsd">
?? ??? ??? ?? <display-name></display-name>?? ?
?? ??? ??? ?? <welcome-file-list>
?? ??? ??? ??? ?<welcome-file>login.jsp</welcome-file>
?? ??? ??? ?? </welcome-file-list>
?? ??? ??? ?? <filter>
?? ??? ??? ??? ?<filter-name>requestFilter</filter-name>
?? ??? ??? ??? ?<filter-class>cn.jbit.mvc.ActionFIlter</filter-class>
?? ??? ??? ?? </filter>
?? ??? ??? ?? <filter-mapping>
?? ??? ??? ??? ?<filter-name>requestFilter</filter-name>
?? ??? ??? ??? ?<url-pattern>*.action</url-pattern>
?? ??? ??? ?? </filter-mapping>
?? ??? ??? ?</web-app>
1.6 升級(jí)Controller控制器
?? ??? ?1、配置文件
?? ??? ??? ?myStruts.xml
?? ??? ??? ?<?xml version="1.0" encoding="UTF-8"?>
?? ??? ??? ??? ?<!DOCTYPE mystruts[
?? ??? ??? ??? ??? ?<!ELEMENT mystruts (actions)>
?? ??? ??? ??? ??? ?<!ELEMENT actions (action*)>
?? ??? ??? ??? ??? ?<!ELEMENT action (result*)>
?? ??? ??? ??? ??? ?<!ATTLIST auction
?? ??? ??? ??? ??? ??? ?name CDATA #REQUIRED
?? ??? ??? ??? ??? ??? ?CLASS CDATA #REQUIRED>
?? ??? ??? ??? ??? ?<!ATTLIST result
?? ??? ??? ??? ??? ??? ?name CDATA #IMPLIED
?? ??? ??? ??? ??? ??? ?redirect (true|false) "false">
?? ??? ??? ??? ?]>
?? ??? ??? ??? ?<mystruts>
?? ??? ??? ??? ??? ?<actions>
?? ??? ??? ??? ??? ??? ?<action name="register" class="cn.jbit.action.RegisterAction">
?? ??? ??? ??? ??? ??? ??? ?<result name="success">page/login.jsp</result>
?? ??? ??? ??? ??? ??? ??? ?<result name="input">page/register.jsp</result>
?? ??? ??? ??? ??? ??? ?</action>
?? ??? ??? ??? ??? ??? ?<action name="login" class="cn.jbit.mvc.LoginAction">
?? ??? ??? ??? ??? ??? ??? ?<result name="success">page/guanli.jsp</result>
?? ??? ??? ??? ??? ??? ??? ?<result name="input">page/login.jsp</result>
?? ??? ??? ??? ??? ??? ?</action>
?? ??? ??? ??? ??? ?</actions>
?? ??? ??? ??? ?</mystruts>
?? ??? ??? ?2、保存Action信息
?? ??? ??? ??? ?public class ActionMapping {
?? ??? ??? ??? ??? ?//action元素的name屬性
?? ??? ??? ??? ??? ?private String name;
?? ??? ??? ??? ??? ?//action元素的className屬性
?? ??? ??? ??? ??? ?private String className;
?? ??? ??? ??? ??? ?//保存配置愛的result屬性信息
?? ??? ??? ??? ??? ?private Map<String, String > resultMap=new HashMap<String, String>();
?? ??? ??? ??? ??? ?public String getName() {
?? ??? ??? ??? ??? ??? ?return name;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?public void setName(String name) {
?? ??? ??? ??? ??? ??? ?this.name = name;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?public String getClassName() {
?? ??? ??? ??? ??? ??? ?return className;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?public void setClassName(String className) {
?? ??? ??? ??? ??? ??? ?this.className = className;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?public Map<String, String> getResultMap() {
?? ??? ??? ??? ??? ??? ?return resultMap;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?public void setResultMap(Map<String, String> resultMap) {
?? ??? ??? ??? ??? ??? ?this.resultMap = resultMap;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?/*
?? ??? ??? ??? ??? ? * 根據(jù)result-name返回Result實(shí)例
?? ??? ??? ??? ??? ? */
?? ??? ??? ??? ??? ?public String getResult(String name,String result){
?? ??? ??? ??? ??? ??? ?return resultMap.get(name);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?public String getResult(String name){
?? ??? ??? ??? ??? ??? ?return resultMap.get(name);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?/*
?? ??? ??? ??? ??? ? * 向Map中添加一個(gè)View
?? ??? ??? ??? ??? ? */
?? ??? ??? ??? ??? ?public void addResult(String name,String result){
?? ??? ??? ??? ??? ??? ?this.resultMap.put(name, result);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?3、讀取配置文件
?? ??? ??? ??? ?public class ActionMappingManager {
?? ??? ??? ??? ??? ?//保存所有Action的ActionMapping
?? ??? ??? ??? ??? ?private static Map<String,ActionMapping> actionMappings=new HashMap<String, ActionMapping>();
?? ??? ??? ??? ??? ?/**
?? ??? ??? ??? ??? ? * init方法用來加載Action配置文件
?? ??? ??? ??? ??? ? * @param configureFileName 配置文件名
?? ??? ??? ??? ??? ? */
?? ??? ??? ??? ??? ?public void init(String configureFileName){
?? ??? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ??? ?if(configureFileName==null || configureFileName.isEmpty()){
?? ??? ??? ??? ??? ??? ??? ??? ?throw new Exception("ConfigureFileName為空");
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?InputStream is=this.getClass().getResourceAsStream("/"+configureFileName);
?? ??? ??? ??? ??? ??? ??? ?Document doc=new SAXReader().read(is);
?? ??? ??? ??? ??? ??? ??? ?Element root=doc.getRootElement();
?? ??? ??? ??? ??? ??? ??? ?Iterator<Element> actionsIt=root.elements("actions").iterator();
?? ??? ??? ??? ??? ??? ??? ?Element actions =actionsIt.next();
?? ??? ??? ??? ??? ??? ??? ?for(Iterator<Element> actionIt=actions.elementIterator("action");actionIt.hasNext();){
?? ??? ??? ??? ??? ??? ??? ??? ?Element action=actionIt.next();
?? ??? ??? ??? ??? ??? ??? ??? ?ActionMapping mapping=new ActionMapping();
?? ??? ??? ??? ??? ??? ??? ??? ?mapping.setName(action.attributeValue("name"));
?? ??? ??? ??? ??? ??? ??? ??? ?mapping.setClassName(action.attributeValue("class"));
?? ??? ??? ??? ??? ??? ??? ??? ?for(Iterator<Element> resultIt=action.elementIterator("result");resultIt.hasNext();){
?? ??? ??? ??? ??? ??? ??? ??? ??? ?Element resultElement=resultIt.next();
?? ??? ??? ??? ??? ??? ??? ??? ??? ?String name=resultElement.attributeValue("name");
?? ??? ??? ??? ??? ??? ??? ??? ??? ?String result=resultElement.getText();
?? ??? ??? ??? ??? ??? ??? ??? ??? ?if(name==null || "".equals(name)){
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?name="success";
?? ??? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ??? ?mapping.addResult(name, result);
?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ?actionMappings.put(mapping.getName(), mapping);
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?/**
?? ??? ??? ??? ??? ? * 加載Action配置文件
?? ??? ??? ??? ??? ? * @param configureFileNames 配置文件名的數(shù)組
?? ??? ??? ??? ??? ? */
?? ??? ??? ??? ??? ?public ActionMappingManager(String[] configureFileNames){
?? ??? ??? ??? ??? ??? ?for(String configureFileName:configureFileNames){
?? ??? ??? ??? ??? ??? ??? ?init(configureFileName);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?/**
?? ??? ??? ??? ??? ? * 根據(jù)actionName查詢對(duì)應(yīng)的ActionMapping實(shí)例
?? ??? ??? ??? ??? ? * @param actionName
?? ??? ??? ??? ??? ? * @return
?? ??? ??? ??? ??? ? * @throws Exception
?? ??? ??? ??? ??? ? */
?? ??? ??? ??? ??? ?public ActionMapping getActionMappingByName(String actionName)throws Exception{
?? ??? ??? ??? ??? ??? ?if(actionName==null || actionName.isEmpty()){
?? ??? ??? ??? ??? ??? ??? ?return null;
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?ActionMapping mapping=this.actionMappings.get(actionName);
?? ??? ??? ??? ??? ??? ?if(mapping==null){
?? ??? ??? ??? ??? ??? ??? ?throw new Exception("mapping為空:["+actionName+"]");
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?return mapping;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?4、反射生成Action
?? ??? ??? ??? ?public class ActionManager {
?? ??? ??? ??? ??? ?public static Action createAction(String className)throws Exception{
?? ??? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ??? ?return (Action)loadClass(className).newInstance();
?? ??? ??? ??? ??? ??? ?}catch (ClassNotFoundException e) {
?? ??? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}catch (InstantiationException e) {
?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}catch (IllegalAccessException e) {
?? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?return null;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?private static Class loadClass(String className) throws ClassNotFoundException {
?? ??? ??? ??? ??? ??? ?Class clazz=null;
?? ??? ??? ??? ??? ??? ?clazz=Class.forName(className);
?? ??? ??? ??? ??? ??? ?return clazz;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?5、完善Controller
?? ??? ??? ??? ?web.xml:
?? ??? ??? ??? ?<filter>
?? ??? ??? ??? ??? ?<display-name>requestFilter</display-name>
?? ??? ??? ??? ??? ?<filter-name>requestFilter</filter-name>
?? ??? ??? ??? ??? ?<filter-class>cn.jbit.mvc.ActionFIlter</filter-class>
?? ??? ??? ??? ??? ?<init-param>
?? ??? ??? ??? ??? ??? ?<param-name>config</param-name>
?? ??? ??? ??? ??? ??? ?<param-value>myMvc.xml</param-value>
?? ??? ??? ??? ??? ?</init-param>
?? ??? ??? ??? ?? </filter>
?? ??? ??? ?ActionFilter.java修改:?? ?
?? ??? ??? ??? ??? ?public class ActionFilter implements Filter {
?? ??? ??? ??? ??? ??? ?private FilterConfig config;
?? ??? ??? ??? ??? ??? ?private ActionMappingManager mappingManager;
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void destroy() {
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void doFilter(ServletRequest request, ServletResponse response,
?? ??? ??? ??? ??? ??? ??? ??? ?FilterChain chain) throws IOException, ServletException {
?? ??? ??? ??? ??? ??? ??? ?//將請(qǐng)求裝換成HttpServletRequest
?? ??? ??? ??? ??? ??? ??? ?HttpServletRequest hsr=(HttpServletRequest) request;
?? ??? ??? ??? ??? ??? ??? ?HttpServletResponse hsp=(HttpServletResponse) response;
?? ??? ??? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ??? ??? ?ActionMapping mapping=this.getActionMapping(hsr);
?? ??? ??? ??? ??? ??? ??? ??? ?Action action=ActionManager.createAction(mapping.getName());
?? ??? ??? ??? ??? ??? ??? ??? ?//得到結(jié)果的邏輯名
?? ??? ??? ??? ??? ??? ??? ??? ?String resultName=action.execute(hsr, hsp);
?? ??? ??? ??? ??? ??? ??? ??? ?//根據(jù)邏輯名返回實(shí)際跳轉(zhuǎn)的視圖名,也就是跳轉(zhuǎn)的路徑
?? ??? ??? ??? ??? ??? ??? ??? ?String result=mapping.getResult(resultName);
?? ??? ??? ??? ??? ??? ??? ??? ?if(result==null){
?? ??? ??? ??? ??? ??? ??? ??? ??? ?return;
?? ??? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??? ?//頁面跳轉(zhuǎn)
?? ??? ??? ??? ??? ??? ??? ??? ?hsp.sendRedirect(result);
?? ??? ??? ??? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ??? ??? ??? ?// TODO: handle exception
?? ??? ??? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?@Override
?? ??? ??? ??? ??? ??? ?public void init(FilterConfig conf) throws ServletException {
?? ??? ??? ??? ??? ??? ??? ?this.config=conf;
?? ??? ??? ??? ??? ??? ??? ?String conStr=config.getInitParameter("config");
?? ??? ??? ??? ??? ??? ??? ?//可包含多個(gè)配置文件
?? ??? ??? ??? ??? ??? ??? ?String[] configFiles=null;
?? ??? ??? ??? ??? ??? ??? ?if(conStr==null||conStr.isEmpty()){
?? ??? ??? ??? ??? ??? ??? ??? ?configFiles=new String[]{"myMvc.xml"};
?? ??? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ??? ?//拆分配置文件名稱字符串
?? ??? ??? ??? ??? ??? ??? ??? ?configFiles=conStr.split(",");
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ?this.mappingManager=new ActionMappingManager(configFiles);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?private ActionMapping getActionMapping(HttpServletRequest request) throws Exception {
?? ??? ??? ??? ??? ??? ??? ?//獲取請(qǐng)求路徑
?? ??? ??? ??? ??? ??? ??? ?String uri=((HttpServletRequest)request).getRequestURI();
?? ??? ??? ??? ??? ??? ??? ?String contextPath=((HttpServletRequest)request).getContextPath();
?? ??? ??? ??? ??? ??? ??? ?//截取上下文路勁后面的部分
?? ??? ??? ??? ??? ??? ??? ?String actionPath=uri.substring(contextPath.length());
?? ??? ??? ??? ??? ??? ??? ?//獲取Action名稱
?? ??? ??? ??? ??? ??? ??? ?String actionName=actionPath.substring(1,actionPath.lastIndexOf(".")).trim();
?? ??? ??? ??? ??? ??? ??? ?ActionMapping mapping=null;
?? ??? ??? ??? ??? ??? ??? ?mapping=mappingManager.getActionMappingByName(actionName);
?? ??? ??? ??? ??? ??? ??? ?return mapping;
?? ??? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
轉(zhuǎn)載于:https://www.cnblogs.com/luobailin/p/6093116.html
總結(jié)
以上是生活随笔為你收集整理的第一章 自定义MVC框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从点亮一个LED开始,Cortex-A9
- 下一篇: Ubuntu中设置静态IP和DNS