login implemented with hibernate
/trunk/src/struts.xml |
---|
6,16 → 6,11 |
<package name="default" extends="hibernate-default"> |
<action name="LoginDo" class="ch.ffhs.webE.action.LoginAction"> |
<result name="success" type="redirect">/user_index.jsp</result> |
<action name="LoginDo" method="verifyUser" class="ch.ffhs.webE.action.LoginAction"> |
<result name="admin" type="redirect">/admin_index.jsp</result> |
<result name="user" type="redirect">/user_index.jsp</result> |
<result name="failed" type="redirect">/index.jsp</result> |
</action> |
<action name="addUser" method="add" class="ch.ffhs.webE.web.UserAction"> |
<result name="success" type="redirect">listUser</result> |
</action> |
<action name="listUser" method="list" class="ch.ffhs.webE.web.UserAction"> |
<result name="success">/register.jsp</result> |
</action> |
</package> |
</struts> |
/trunk/src/ch/ffhs/webE/dao/UserDAO.java |
---|
8,5 → 8,5 |
List<User> listUser(); |
void saveUser(User user); |
User searchUsername(String username); |
} |
/trunk/src/ch/ffhs/webE/dao/UserDAOImpl.java |
---|
10,6 → 10,7 |
import ch.ffhs.webE.domain.*; |
public class UserDAOImpl implements UserDAO { |
@SessionTarget |
Session session; |
@TransactionTarget |
36,4 → 37,24 |
e.printStackTrace(); |
} |
} |
/** |
* Returns a single user with this user name (used for login) |
* @param username: String - entire user name |
* @return User: Returns a user object if something is found. If not, null is returned |
*/ |
public User searchUsername(String username) { |
User user = null; |
try { |
user = (User) session.createQuery( |
"FROM User " + |
"WHERE username = :username") |
.setParameter("username", username) |
.uniqueResult(); |
} catch (Exception e) { |
//TODO: Log error |
} |
return user; |
} |
} |
/trunk/src/ch/ffhs/webE/action/LoginAction.java |
---|
1,23 → 1,45 |
package ch.ffhs.webE.action; |
import ch.ffhs.webE.dao.UserDAO; |
import ch.ffhs.webE.dao.UserDAOImpl; |
import ch.ffhs.webE.domain.User; |
import com.opensymphony.xwork2.ActionSupport; |
import com.opensymphony.xwork2.ModelDriven; |
public class LoginAction extends ActionSupport { |
public class LoginAction extends ActionSupport implements ModelDriven<User>{ |
private static final long serialVersionUID = 1799753056277211344L; |
private User user = new User(); |
private UserDAO userDAO = new UserDAOImpl(); |
private String userName ; |
private String password; |
private String pw; |
public LoginAction() { |
} |
public String execute() { |
//TODO: DB Abfrage für Login |
if("moos".equals(getUserName())) { |
return "success"; |
public String verifyUser() { |
//If password or user name are empty, the login fails. |
if("".equals(getUserName()) || "".equals(getPw()) || getUserName() == null || getPw() == null) { |
return "failed"; |
} |
User u = userDAO.searchUsername(getUserName()); |
if(u == null) |
return "failed"; |
if(!u.getPassword().equals(getPw())) |
return "failed"; |
if(u.isAdmin()) { |
return "admin"; |
} else { |
return "failed"; |
return "user"; |
} |
} |
28,12 → 50,18 |
public void setUserName(String userName) { |
this.userName = userName; |
} |
public String getPw() { |
return pw; |
} |
public String getPassword() { |
return password; |
public void setPw(String pw) { |
this.pw = pw; |
} |
public void setPassword(String password) { |
this.password = password; |
@Override |
public User getModel() { |
return user; |
} |
/trunk/src/hibernate.cfg.xml |
---|
12,8 → 12,22 |
<property name="connection.pool_size">1</property> |
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> |
<!-- Print all generated SQL to the console --> |
<property name="show_sql">true</property> |
<property name="hbm2ddl.auto">create</property> |
<property name="format_sql">true</property> |
<!-- |
<property name="connection.provider_class"> |
org.hibernate.connection.C3P0ConnectionProvider |
</property>--> |
<!-- C3P0 connection pool |
<property name="hibernate.c3p0.min_size">5</property> |
<property name="hibernate.c3p0.max_size">20</property> |
<property name="hibernate.c3p0.timeout">300</property> |
<property name="hibernate.c3p0.max_statements">50 </property> |
<property name="hibernate.c3p0.idle_test_period">3000 </property> |
--> |
<!--<property name="hbm2ddl.auto">create</property>--> |
<mapping class="ch.ffhs.webE.domain.User" /> |
</session-factory> |
</hibernate-configuration> |
/trunk/WebContent/login.jsp |
---|
4,7 → 4,7 |
<s:form action="LoginDo"> |
<s:textfield name="userName" label="Benutzername" /> |
<s:password name="password" label="Passwort" /> |
<s:password name="pw" label="Passwort" /> |
<s:submit /> |
</s:form> |
<p> |