关于ModelDriven如何在同一个actiong中实现两个类的问题

just_learner007 2015-11-13 09:22:30
做一个登录系统,其中有教师登录和学生登录两个角色,我已经创建了teacher类和student类,想用ModelDriven传值到action,可是不知道应该怎样才能将这两个类同时实现,特来请教各位,我应该怎么做?下边附上源码
Teacher.java类
[code=text]package com.hp.exa.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student{

private String studentID;
private String password;
private String studentName;
private int result;
private String sclass;
@Id
@GeneratedValue
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}


}
[/code]
Student.java类
package com.hp.exa.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="teacher")
public class Teacher{
private String teacherID;
private String password;
@Id
@GeneratedValue
public String getTeacherID() {
return teacherID;
}
public void setTeacherID(String teacherID) {
this.teacherID = teacherID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

LoginAction.类
package com.hp.exa.action;

import com.hp.exa.dao.LoginDao;
import com.hp.exa.dao.impl.LoginDaoImpl;
import com.hp.exa.hibernate.Student;
import com.hp.exa.hibernate.Teacher;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class LoginAction extends ActionSupport implements ModelDriven<Student>{

/**
*
*/
private static final long serialVersionUID = 1L;
private Teacher teacher;
private Student student;
private String role;

public String execute() {
if ("student".equals(role)) {
LoginDao logindao = new LoginDaoImpl();
if (logindao.studentlogin(student)) {
return "studentsuccess";
} else {
return ERROR;
}
}else if("teacher".equals(role)){
LoginDao logindao=new LoginDaoImpl();
if(logindao.teacherlogin(teacher)){
return "teachersuccess";
}else{
return ERROR;
}
}
return role;

}

@Override
public Student getModel() {
// TODO Auto-generated method stub
if (student == null) {
student = new Student();
}
return student;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

}
...全文
186 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
just_learner007 2015-11-13
  • 打赏
  • 举报
回复
@花谢尊前不敢香 是这样写吗
package com.hp.exa.hibernate;

public class Login {
	private Student student;
	private Teacher teacher;

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

}
  • 打赏
  • 举报
回复
再另外写一个类如Test,属性有teacher和student,提供set get 在getModel里 return new Test(),前台页面用teacher.teacherID等传值。
just_learner007 2015-11-13
  • 打赏
  • 举报
回复
@花谢尊前不敢香 具体应该怎么做?刚开始学不太懂
just_learner007 2015-11-13
  • 打赏
  • 举报
回复
@just_learner007 具体应该怎么做?我刚开始学java,还不太懂
just_learner007 2015-11-13
  • 打赏
  • 举报
回复
具体应该怎么做啊?刚开始学习java,还不懂应该怎么去做
引用
再抽出来一个类,存放teacher和student对象,驱动这个类。
  • 打赏
  • 举报
回复
再抽出来一个类,存放teacher和student对象,驱动这个类。
  • 打赏
  • 举报
回复
大胆尝试,主动验证。结果自己就能看到了。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧