初学ssh框架整合小项目--老师学生成绩管理系统
前天学习了关于三个框架的整合,主要spring-hibernate-struts1,因为没学struts2所以暂时是用1做的
一个很简单的项目---老师学生成绩管理系统
先说三个框架的整合:
1.导入三个框架的jar包;
2.拷贝hibernate.cfg.xml文件到SRC目录下,拷贝spring的配置文件applicationContext.xml和struts1的配置文件放入到WEB-INF目录下 <listener>
3.在web.xml文件里加入下面这个监听器
?
?下面是applicationContext
?
?model类和映射的文件包括hibernate的配置文件都可以自动生成还可以用DB反向生成,这里就不放配置文件了。
注:在struts1中的配置文件路径后的type应该是
?
??<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"parameter="action">
? <forward name="success" path="/welcome.jsp"></forward>
? <forward name="fail" path="/index.jsp"></forward>
? </action>
接下来就是编写业务的代码:service主要是对业务的逻辑处理 ?dao层主要处理数据库 比较简单
主要还是action中
?
import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.DispatchAction;import com.aowin.model.Score;import com.aowin.model.Student;import com.aowin.model.Sysuser;import com.aowin.service.ScoreService;import com.aowin.service.StudentService;import com.aowin.service.SysuserService;// 在action中调用scoreservice的方法public class LoginAction extends DispatchAction {private StudentService studentService ;private SysuserService sysuserService;private ScoreService scoreService;public ScoreService getScoreService() {return scoreService;}public void setScoreService(ScoreService scoreService) {this.scoreService = scoreService;}public SysuserService getSysuserService() {return sysuserService;}public void setSysuserService(SysuserService sysuserService) {this.sysuserService = sysuserService;}public StudentService getStudentService() {return studentService;}public void setStudentService(StudentService studentService) {this.studentService = studentService;}//检查用户是否在数据库中存在public ActionForward checkUser(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {String username = request.getParameter("username");String password = request.getParameter("password");String radio = request.getParameter("aaa");request.setAttribute("radio", radio);List<Score> list = scoreService.getALLScore();request.setAttribute("list", list);if("3".equals(radio)){Student student = studentService.checkStudent(username, password);if(student==null){request.setAttribute("usernameerror", "your username is error");//Map key valuereturn mapping.findForward("fail");}else{List<Score> stulist = scoreService.getScoreByStuid(student.getId());request.setAttribute("stulist", stulist);request.setAttribute("username", username);return mapping.findForward("studentView");}}else if("1".equals(radio)){Sysuser sysuser = sysuserService.checkSysuserIsExist(username, password);if(sysuser==null){request.setAttribute("usernameerror", "your username is error");//Map key valuereturn mapping.findForward("fail");}else{request.setAttribute("username", username);//老师信息的listreturn mapping.findForward("success");}}else{Sysuser sysuser = sysuserService.checkSysuserIsExist(username, password);if(sysuser==null){return mapping.findForward("fail");}else{//显示为老师的信息 管理员管理老师System.out.println(radio);List<Sysuser> teachers = sysuserService.getSysuserList(1);request.setAttribute("teachers", teachers);request.setAttribute("username1", username);return mapping.findForward("teacherView");}}}//增加学生信息时做的操作的actionpublic ActionForward addScoreMessage(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { Integer stuid = Integer.valueOf(request.getParameter("stuid"));Integer couid = Integer.valueOf(request.getParameter("couid"));String score = request.getParameter("score");if(scoreService.IsNotExist(stuid, couid)){scoreService.addScore(stuid, couid, score);List<Score> list = scoreService.getALLScore();request.setAttribute("list", list);return mapping.findForward("success");}else{return mapping.findForward("error");}}//更新学生的信息public ActionForward updateScoreMessage(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { Integer id = Integer.valueOf(request.getParameter("scoreid"));Integer stuid = Integer.valueOf(request.getParameter("stuid"));Integer couid = Integer.valueOf(request.getParameter("couid"));String score = request.getParameter("score");scoreService.updateScore(id, stuid, couid, score);List<Score> list = scoreService.getALLScore();request.setAttribute("list", list);return mapping.findForward("success");}//删除学生的信息public ActionForward delScore(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { String id = request.getParameter("id");int scoreId = Integer.valueOf(id);System.out.println(scoreId);scoreService.deleteScore(scoreId);List<Score> list = scoreService.getALLScore();request.setAttribute("list", list);return mapping.findForward("success");}//点击增加时先进入这个action 在通过addInput转到增加的界面上public ActionForward addInput(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { System.out.println("done");return mapping.findForward("addInput");}//增加老师的信息的action 跳转到的时候直接跳到jsppublic ActionForward addTeacher(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { Integer type = Integer.valueOf(request.getParameter("type"));String username = request.getParameter("username");String logonname = request.getParameter("logonname");String password = request.getParameter("password");sysuserService.saveSysuser(type, username, logonname, password);List<Sysuser> teachers = sysuserService.getSysuserList(1);request.setAttribute("teachers", teachers);return mapping.findForward("teacherView");//如果这个老师已经存在的话就不能添加了 这个暂时不管先}public ActionForward delTeacher(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { Integer id = Integer.valueOf(request.getParameter("id"));sysuserService.delTeacherMessageById(id);List<Sysuser> teachers = sysuserService.getSysuserList(1);request.setAttribute("teachers", teachers);return mapping.findForward("teacherView");}public ActionForward updateTeacher(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception { Integer sysuserid = Integer.valueOf(request.getParameter("sysuserid"));String username = request.getParameter("username");String logonname = request.getParameter("logonname");sysuserService.updateTeacherMessage(sysuserid, username, logonname);List<Sysuser> teachers = sysuserService.getSysuserList(1);request.setAttribute("teachers", teachers);return mapping.findForward("teacherView");}}?
1 楼 lxc19881231 2012-06-07 Struts1真麻烦。还是2用的爽。哈哈 2 楼 chenyingjie1001 2012-06-07 lxc19881231 写道Struts1真麻烦。还是2用的爽。哈哈
后面还写了个用2的 感觉还不错 代码清晰很多