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

實現login的兩種方法

2013-11-23 19:27:20  來源: Java核心技術 

  方式一將VO的東西封裝到Action裡面

  編寫Action方法

  package action;

  import comopensymphonyxworkActionSupport;

  public class LoginAction extends ActionSupport{

  private String username;

  private String password;

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  @Override

  public String execute() throws Exception {

  if (aaaequalsIgnoreCase(username)&&aaaequalsIgnoreCase(password)) {

  return loginSuc;

  }else {

  return loginFail;

  }

  }

  }

  配置文件

  <action name=mylogin class=actionLoginAction>

  <result name=input>/myloginjsp</result>

  <result name=loginSuc>/okjsp</result>

  <result name=loginFail>/errokjsp</result>

  </action>

  然後編寫登錄的JSP頁面

  <s:form action=myloginaction>

  <table width=% align=center>

  <tr>

  <td>

  <s:textfield key=username label=USERNAME />

  </td>

  </tr>

  <tr>

  <td>

  <s:password key=password label=PASSWORD />

  </td>

  </tr>

  <tr>

  <td>

  <s:submit value=SUBMIT />

  </td>

  </tr>

  </table>

  </s:form>

  注意這裡面的JSP裡面的表單名稱要與Action裡面的名稱保持一致的

  也可以將其抽出來/

  方式二定義一個VO對象

  自定義vo文件名LoginVOjava

  文件內容

  package strutslogin;

  public class LoginVO {

  private String username;

  private String password;

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  在Action文件中要使用這個vo

  文件內容

  package strutslogin;

  public class LoginAction {

  private LoginVO user = null;

  public String execute() {

  Systemoutprintln (LoginActionclasshashCode());

  if (usergetUsername()equalsIgnoreCase(aaa) &&

  usergetPassword()equals(aaaaaa)) {

  return loginSuc;

  }

  else {

  return loginFail;

  }

  }

  public LoginVO getUser() {

  return user;

  }

  public void setUser(LoginVO user) {

  thisuser = user;

  }

  }

  登陸成功的文件如下

  <%@ page contentType=text/html; charset=gb %>

  <%@ taglib uri=/strutstags prefix=s%>

  <meta httpequiv=contenttype content=text/html;charset=gb>

  歡迎您<s:property name=userusername>登錄成功

  注意login文件的部分也要進行修改

  文件內容如下

  <meta httpequiv=contenttype content=text/html;charset=gb>

  <title>login</title>

  <form action=loginaction method=post>

  username:<input type=input name=userusername><br>

  password:<input type=input name=userpassword><br>

  <input type=submit value=登錄>

  </form>


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