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>绍兴县政民互动系统后台</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('验证码不能为空!');
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分辨率以上浏览本系统
      <br>
      Copyright © 2006-2008 浙江榕基信息技术有限公司
     </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">
        验证码:
       </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这个类
找到public void handleRequest(HttpServletRequest request)
throws ServletException方法

DiskFileUpload upload = new DiskFileUpload();

后边加上
upload.setHeaderEncoding(request.getCharacterEncoding());
编译后放到struts.jar包里覆盖原来的
posted on 2009-06-02 10:26 方涛升 阅读(281) 评论(0)  编辑  收藏 所属分类: struts

只有注册用户登录后才能发表评论。


网站导航:
 
<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

搜索

  •  

最新评论

阅读排行榜

评论排行榜