posts - 33,  comments - 11,  trackbacks - 0

          1.index.jsp

          <%@ page language="java" pageEncoding="UTF-8"%>
          <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
           prefix="bean"%>
          <%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
           prefix="html"%>

          <html>
           <head>
            <title>紹興縣政民互動(dòng)系統(tǒng)后臺(tái)</title>
            <link href="css/module.css" rel="stylesheet" type="text/css">
            <link href="css/base.css" rel="stylesheet" type="text/css">
          <script type="text/javascript">
          function beforeSubmit(loginForm){
          if(loginForm.username.value==''){
          alert('用戶名不能為空!');
          loginForm.username.focus();
          return false;
          }
          if(loginForm.password.value==''){
          alert('密碼不能為空!');
          loginForm.password.focus();
          return false;
          }
          if(loginForm.textcode.value==''){
          alert('驗(yàn)證碼不能為空!');
          loginForm.textcode.focus();
          return false;
          }
          return true;
          }
          </script>
           </head>
           <body id="mybody" class="loginbody"
            onKeyDown="if(event.keyCode==13)loginFunc();">

            <div class="logintitle">
             <img src="images/login_06.gif">
            </div>
            <div class="rjlogo">
             <table border="0" cellspacing="0" cellpadding="0">
              <tr>
               <td valign="bottom">
                建議使用Internet Explorer 6.0版本瀏覽器、1024*768分辨率以上瀏覽本系統(tǒng)
                <br>
                Copyright © 2006-2008 浙江榕基信息技術(shù)有限公司
               </td>
               <td>
                <img src="images/login_10.gif">
               </td>
              </tr>
             </table>
            </div>
            <div class="loginbg">
             <div class="loginform">
              <html:form action="/login?method=login"
               onsubmit="return beforeSubmit(this);" method="post" enctype="multipart/form-data">
               <table width="200" border="0" cellpadding="1" cellspacing="0"
                id="ta1">
                <tr>
                 <td align="right">
                  英文名稱:
                 </td>
                 <td>
                  <html:text property="username"
                   style=" width:120px; height:18px; border:#FFFFFF solid 1px;" />
                 </td>
                </tr>
                <tr>
                 <td align="right">
                  用戶密碼:
                 </td>
                 <td>
                  <html:password property="password"
                   style=" width:120px; height:18px; border:#FFFFFF solid 1px;" />
                 </td>
                </tr>
                <tr>
                 <td align="right">
                  驗(yàn)證碼:
                 </td>
                 <td>
                  <html:text property="textcode"
                   style="width:60px;height:18px; border:#FFFFFF solid 1px;"
                   maxlength="4" /><html:image src="image.jsp" border="0" align="absmiddle"></html:image>
                 </td>
                </tr>
                <tr>
                 <td align="right">
                  上傳圖片:
                 </td>
                 <td>
                  <html:file property="file" value="瀏覽"
                   style="width:30px;height:18px; border:#FFFFFF solid 1px;"
                   maxlength="4" />
                 </td>
                </tr>
                <tr>
                <td>&nbsp;</td>
                 <td align="left">
                  <html:submit
                   style="background:url(images/login_05.gif); width:76px; height:26px; border:0;"
                   value="登陸"></html:submit>
                 </td>
                </tr>
               </table>
              </html:form>
             </div>
            </div>
           </body>
          </html>

          2.LoginForm.java

          /*
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           */
          package web.form;

          import javax.servlet.http.HttpServletRequest;
          import org.apache.struts.action.ActionErrors;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.upload.FormFile;

          /**
           * MyEclipse Struts Creation date: 05-19-2009
           *
           * XDoclet definition:
           *
           * @struts.form name="loginForm"
           */
          @SuppressWarnings("serial")
          public class LoginForm extends ActionForm {
           /*
            * Generated fields
            */

           /** username property */
           private String username;

           /** password property */
           private String password;

           /** textcode property */
           private String textcode;

           /** FormFile property */
           private FormFile file;

           /*
            * Generated Methods
            */

           /**
            * Method validate
            *
            * @param mapping
            * @param request
            * @return ActionErrors
            */
           public ActionErrors validate(ActionMapping mapping,
             HttpServletRequest request) {
            // TODO Auto-generated method stub
            return null;
           }

           /**
            * Method reset
            *
            * @param mapping
            * @param request
            */
           public void reset(ActionMapping mapping, HttpServletRequest request) {
            // TODO Auto-generated method stub
           }

           public String getPassword() {
            return password;
           }

           public void setPassword(String password) {
            this.password = password;
           }

           public String getUsername() {
            return username;
           }

           public void setUsername(String username) {
            this.username = username;
           }

           public String getTextcode() {
            return textcode;
           }

           public void setTextcode(String textcode) {
            this.textcode = textcode;
           }

           public FormFile getFile() {
            return file;
           }

           public void setFile(FormFile file) {
            this.file = file;
           }

           /**
            * Returns the username.
            *
            * @return String
            */

          }

          3.loginAction

          /*
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           */
          package web.action;

          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;

          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.actions.DispatchAction;
          import web.form.LoginForm;
          import service.Service;
          import vo.User;

          /**
           * MyEclipse Struts Creation date: 09-27-2006
           *
           * XDoclet definition:
           *
           * @struts.action path="/login" name="loginForm" input="/login.jsp"
           *                scope="request" validate="true"
           * @struts.action-forward name="fail" path="/login.jsp"
           * @struts.action-forward name="succeed" path="/welcome.jsp"
           */
          public class LoginAction extends DispatchAction {
           /*
            * Generated Methods
            */
           /**
            * Method execute
            *
            * @param mapping
            * @param form
            * @param request
            * @param response
            * @return ActionForward
            */
           private Service service;

           public void setService(Service service) {
            this.service = service;
           }

           public Service getService() {
            return service;
           }


           public ActionForward login(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response) {
            LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method
            // stub
            
            
            
            User user = new User();
            user.setPassword(loginForm.getPassword());
            user.setUsername(loginForm.getUsername());

            String filepath = "D:/Upload\\";

            if (!new File(filepath).isDirectory()) {
             new File(filepath).mkdir();
            }

            String file_hz=loginForm.getFile().getFileName();

            String file_name = filepath +file_hz;
            System.out.println(file_name);

            FileOutputStream out;
            try {
             out = new FileOutputStream(new File(file_name));
             out.write(loginForm.getFile().getFileData());
             out.close();
            } catch (FileNotFoundException e) {
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
            }

            String code = "";
            code = (String) request.getSession().getAttribute("rand");
            if (code.trim().equals(loginForm.getTextcode())) {
             System.out.print("ok1");
             if (service.isValid(user)) {
              System.out.print("ok");
              return mapping.findForward("succeed");
             } else {
              System.out.print("no");
              return mapping.findForward("fail");
             }
            } else {
             return mapping.findForward("fail");
            }
           }
          }


          4.取得文件名亂碼解決
          重寫org.apache.struts.upload.CommonsMultipartRequestHandler這個(gè)類
          找到public void handleRequest(HttpServletRequest request)
          throws ServletException方法

          DiskFileUpload upload = new DiskFileUpload();

          后邊加上
          upload.setHeaderEncoding(request.getCharacterEncoding());
          編譯后放到struts.jar包里覆蓋原來的
          posted on 2009-06-02 10:26 方濤升 閱讀(286) 評(píng)論(0)  編輯  收藏 所屬分類: struts

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 田东县| 彭山县| 泰兴市| 泌阳县| 霍林郭勒市| 奉新县| 永仁县| 呼伦贝尔市| 文山县| 教育| 武清区| 华容县| 怀化市| 宜州市| 永登县| 怀柔区| 景东| 安多县| 安岳县| 淮滨县| 鄂托克前旗| 长宁区| 镇赉县| 思茅市| 从化市| 盖州市| 塘沽区| 濮阳县| 烟台市| 旬阳县| 峡江县| 临夏市| 永丰县| 贵阳市| 子洲县| 张家口市| 达日县| 枣庄市| 荣昌县| 长宁县| 石泉县|