使用会话访问struts 2中的实体bean数据的标准过程是什么?

时间:2014-07-17 07:14:17

标签: java hibernate jsp struts2

假设我有一个登录表单作为index.jsp

<h:form action="LoginAction" >
<h:textfield name="user" label="Login ID" /></br>
<h:password name="password" label="Password"/></br>
<h:submit value="LOGIN"/>
</h:form>

这是LoginAction.java

package action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dao.DBService;
import entity.Student;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.dispatcher.SessionMap;


public class LoginAction extends ActionSupport {

HttpServletRequest req;
private String user;
private String password;
Map <String,Object> session;
public void setSession(Map<String, Object> session) {
    this.session = session;

}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}
public boolean validat() throws SQLException 
{
    String query;
    query="select * from student where user='"+user+"' and password='"+password+"'";
    ResultSet rs;
    rs=DBService.selectData(query);
    boolean status=rs.next();
    return status;

}

public String execute() throws Exception  {

    if(validat())
    {

    return SUCCESS;
    }
    return ERROR;
}
public void setServletRequest(HttpServletRequest hsr) {
    req = hsr;
}
}

这是student.java

package entity;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {
private String name;
private String user;
private String password;
private String number;
private String country;
private String email;
private String city;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
@Id
public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getPassword() {
    return password;
}

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

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

}

现在我们有一个jsp作为Profile.jsp 在此页面中,我想显示已登录的学生的所有信息

一种方法是使用 &#39;#session.context&#39; ,但我只能将其用于用户和密码。如何使用会话访问该特定学生的数据库中的所有数据。

1 个答案:

答案 0 :(得分:2)

当您在ResultSet rs中得到结果时,您现在可以访问数据库属性。 就像你有属性&#34; role&#34;一样,你可以像

那样访问它
String r=rs.getString("role");

并在会话中使用

session = ActionContext.getContext().getSession();
session.put("z",r)  // value of string r in z

在您的个人资料文件中,使用

访问此角色
"#session.z"