struts2 與spring 相結合做出的登陸例項

terryisme發表於2009-03-05
原始碼如下:
LoginService.java
package org.test.service;
public interface LoginService {
public boolean isLogin(String username,String password);
}

LoginServiceImpl .java

package org.test.service.impl;

import org.test.service.LoginService;

public class LoginServiceImpl implements LoginService {

public boolean isLogin(String username, String password) {
//判斷使用者所以輸入的資料是否為gao,qin,如果是則登陸成功,否則失敗
// TODO Auto-generated method stub
if(username.equals("gao") && password.equals("qin")){
return true;
}
return false;
}

}

LoginAction.java

package org.test.struts2Action;

import org.test.service.LoginService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
private String username;
private String password;
@SuppressWarnings("unused")
private LoginService loginService;
@Override
public String execute() throws Exception {
if(loginService.isLogin(username, password)){
return SUCCESS;
}
return INPUT;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
}


applicationContext.xml

xmlns="
xmlns:xsi="
xsi:schemaLocation=" /spring-beans-2.0.xsd">








struts.xml

br /> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"




/result.jsp
/index.jsp




web.xml

xmlns="
xmlns:xsi="
xsi:schemaLocation="
/web-app_2_4.xsd">

index.jsp


contextConfigLocation
classpath:applicationContext.xml


struts
org.apache.struts2.dispatcher.FilterDispatcher


struts
/*


org.springframework.web.context.ContextLoaderListener



index.jsp





My JSP 'index.jsp' starting page



使用者登陸










result.jsp





My JSP 'index.jsp' starting page



使用者資訊


使用者名稱:
密碼:${requestScope.password }

[@more@]http://www.blogjava.net/qin/archive/2008/09/24/230929.html?opt=admin

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/93029/viewspace-1018121/,如需轉載,請註明出處,否則將追究法律責任。

相關文章