熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts與Velocity的簡單集成

2013-11-23 20:18:59  來源: Java開源技術 

  先當然是簡單的配置webxml與strutsconfigxml文件加入必要的servlet等東西: Xml代碼
    <?xml version= encoding=UTF?>
    <webapp xmlns= xmlns:xsi=instance version= xsi:schemaLocation=app__xsd>

  <servlet>
        <servletname>velocity</servletname>
        <servletclass>orgapachevelocitytoolsviewservletVelocityViewServlet</servletclass>
      </servlet>
      <servlet>
        <servletname>action</servletname>
        <servletclass>orgapachestrutsactionActionServlet</servletclass>
        <initparam>
          <paramname>config</paramname>
          <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
        </initparam>
        <initparam>
          <paramname>debug</paramname>
          <paramvalue></paramvalue>
        </initparam>
        <initparam>
          <paramname>detail</paramname>
          <paramvalue></paramvalue>
        </initparam>
        <loadonstartup></loadonstartup>
      </servlet>

  <servletmapping>
        <servletname>velocity</servletname>
        <urlpattern>*vm</urlpattern>
      </servletmapping>
      <servletmapping>
        <servletname>action</servletname>
        <urlpattern>*do</urlpattern>
      </servletmapping>

  <welcomefilelist>
        <welcomefile>vms/loginvm</welcomefile>
      </welcomefilelist>
    </webapp>

  Strutsconfigxml:

  <?xml version= encoding=UTF?>
    <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

  <strutsconfig>
      <datasources />
      <formbeans >
        <formbean name=loginForm type=comjiestrutsformLoginForm />

  </formbeans>

  <globalexceptions />
      <globalforwards >
        <forward name=success path=/vms/successvm />
        <forward name=fail path=/vms/failvm />
        <forward name=first path=/vms/loginvm />

  </globalforwards>

  <actionmappings >
        <action
          attribute=loginForm
          input=/loginvm
          name=loginForm
          path=/login
          scope=request
          type=comjiestrutsactionLoginAction />

  </actionmappings>

  <messageresources parameter=comjiestrutsApplicationResources />
    </strutsconfig>
    view plaincopy to clipboardprint?
    <?xml version= encoding=UTF?>
    <webapp xmlns= xmlns:xsi=instance version= xsi:schemaLocation=app__xsd>

  <servlet>
        <servletname>velocity</servletname>
        <servletclass>orgapachevelocitytoolsviewservletVelocityViewServlet</servletclass>
      </servlet>
      <servlet>
        <servletname>action</servletname>
        <servletclass>orgapachestrutsactionActionServlet</servletclass>
        <initparam>
          <paramname>config</paramname>
          <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
        </initparam>
        <initparam>
          <paramname>debug</paramname>
          <paramvalue></paramvalue>
        </initparam>
        <initparam>
          <paramname>detail</paramname>
          <paramvalue></paramvalue>
        </initparam>
        <loadonstartup></loadonstartup>
      </servlet>

  <servletmapping>
        <servletname>velocity</servletname>
        <urlpattern>*vm</urlpattern>
      </servletmapping>
      <servletmapping>
        <servletname>action</servletname>
        <urlpattern>*do</urlpattern>
      </servletmapping>

  <welcomefilelist>
        <welcomefile>vms/loginvm</welcomefile>
      </welcomefilelist>
    </webapp>

  Strutsconfigxml:

  <?xml version= encoding=UTF?>
    <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

  <strutsconfig>
      <datasources />
      <formbeans >
        <formbean name=loginForm type=comjiestrutsformLoginForm />

  </formbeans>

  <globalexceptions />
      <globalforwards >
        <forward name=success path=/vms/successvm />
        <forward name=fail path=/vms/failvm />
        <forward name=first path=/vms/loginvm />

  </globalforwards>

  <actionmappings >
        <action
          attribute=loginForm
          input=/loginvm
          name=loginForm
          path=/login
          scope=request
          type=comjiestrutsactionLoginAction />

  </actionmappings>

  <messageresources parameter=comjiestrutsApplicationResources />
    </strutsconfig>

  <?xml version= encoding=UTF?>
    <webapp xmlns= xmlns:xsi=instance version= xsi:schemaLocation=app__xsd>

  <servlet>
        <servletname>velocity</servletname>
        <servletclass>orgapachevelocitytoolsviewservletVelocityViewServlet</servletclass>
      </servlet>
      <servlet>
        <servletname>action</servletname>
        <servletclass>orgapachestrutsactionActionServlet</servletclass>
        <initparam>
          <paramname>config</paramname>
          <paramvalue>/WEBINF/strutsconfigxml</paramvalue>
        </initparam>
        <initparam>
          <paramname>debug</paramname>
          <paramvalue></paramvalue>
        </initparam>
        <initparam>
          <paramname>detail</paramname>
          <paramvalue></paramvalue>
        </initparam>
        <loadonstartup></loadonstartup>
      </servlet>

  <servletmapping>
        <servletname>velocity</servletname>
        <urlpattern>*vm</urlpattern>
      </servletmapping>
      <servletmapping>
        <servletname>action</servletname>
        <urlpattern>*do</urlpattern>
      </servletmapping>

  <welcomefilelist>
        <welcomefile>vms/loginvm</welcomefile>
      </welcomefilelist>
    </webapp>

  Strutsconfigxml:

  <?xml version= encoding=UTF?>
    <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>

  <strutsconfig>
      <datasources />
      <formbeans >
        <formbean name=loginForm type=comjiestrutsformLoginForm />

  </formbeans>

  <globalexceptions />
      <globalforwards >
        <forward name=success path=/vms/successvm />
        <forward name=fail path=/vms/failvm />
        <forward name=first path=/vms/loginvm />

  </globalforwards>

  <actionmappings >
        <action
          attribute=loginForm
          input=/loginvm
          name=loginForm
          path=/login
          scope=request
          type=comjiestrutsactionLoginAction />

  </actionmappings>

  <messageresources parameter=comjiestrutsApplicationResources />
    </strutsconfig>然後是一個用於測試的登錄頁面:loginvm Html代碼
    <html xmlns=>
    <head>
    <meta httpequiv=ContentType content=text/html; charset=gb />
    <title>登錄頁面</title>
    <style type=text/css>
    <!
    STYLE {color: #FF}
    >
    <!
    STYLE {color: red}
    >
    </style>
    </head>

  <body>
    <form id=form name=form method=post action=logindo>
      <label>用戶名
      <input type=text name=userName />
      </label>
      <p>
        <label>密  碼
        <input type=password name=password />
        </label>
      </p>
      <p>
        <label>
        <input type=submit name=Submit value=提交 />
        </label>
      </p>
    </form>
    #if($isSuccess)<p class=STYLE>驗證通過</p>#else  #end
    #if($isNull)<p class=STYLE>字段不能為空</p>#else  #end
    </body>
    </html>
    view plaincopy to clipboardprint?
    <html xmlns=>
    <head>
    <meta httpequiv=ContentType content=text/html; charset=gb />
    <title>登錄頁面</title>
    <style type=text/css>
    <!
    STYLE {color: #FF}
    >
    <!
    STYLE {color: red}
    >
    </style>
    </head>

  <body>
    <form id=form name=form method=post action=logindo>
      <label>用戶名
      <input type=text name=userName />
      </label>
      <p>
        <label>密  碼
        <input type=password name=password />
        </label>
      </p>
      <p>
        <label>
        <input type=submit name=Submit value=提交 />
        </label>
      </p>
    </form>
    #if($isSuccess)<p class=STYLE>驗證通過</p>#else  #end
    #if($isNull)<p class=STYLE>字段不能為空</p>#else  #end
    </body>
    </html>

  <html xmlns=>
    <head>
    <meta httpequiv=ContentType content=text/html; charset=gb />
    <title>登錄頁面</title>
    <style type=text/css>
    <!
    STYLE {color: #FF}
    >
    <!
    STYLE {color: red}
    >
    </style>
    </head>

  <body>
    <form id=form name=form method=post action=logindo>
      <label>用戶名
      <input type=text name=userName />
      </label>
      <p>
        <label>密  碼
        <input type=password name=password />
        </label>
      </p>
      <p>
        <label>
        <input type=submit name=Submit value=提交 />
        </label>
      </p>
    </form>
    #if($isSuccess)<p class=STYLE>驗證通過</p>#else  #end
    #if($isNull)<p class=STYLE>字段不能為空</p>#else  #end
    </body>
    </html>還有一個後台處理的LoginActionjava: Java代碼
    public ActionForward execute(ActionMapping mapping ActionForm form
                            HttpServletRequest request HttpServletResponse response) {
                    LoginForm loginForm = (LoginForm) form;// TODO Autogenerated method stub
                    String userName = loginFormgetUserName();
                    String password = loginFormgetPassword();

  if(userNameequals(jie) && passwordequals())
                    {
                            isSuccess = true;
                            requestsetAttribute(isSuccess isSuccess);//集成的關鍵點在這裡把對象加入到vm模板                            return mappingfindForward(first);         裡面然後可以在模板裡面用 $isSuccess 來使用

  }else if(userName== || password==)
                    {
                            boolean isNull = true;
                            requestsetAttribute(isNull isNull);
                            return mappingfindForward(first);
                    }
                    return mappingfindForward(fail);

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28282.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.