前天学习了关于三个框架的整合,主要spring-hibernate-struts1,因为没学struts2所以暂时是用1做的
一个很简单的项目---老师学生成绩管理系统
先说三个框架的整合:
1.导入三个框架的jar包;
2.拷贝hibernate.cfg.xml文件到SRC目录下,拷贝spring的配置文件applicationContext.xml和struts1的配置文件放入到WEB-INF目录下 <listener>
3.在web.xml文件里加入下面这个监听器
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
下面是我的两个spring配置文件的
这个是applicationContext_dao_service_action导入到applicationContext中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="/login" class="com.aowin.action.LoginAction">
<property name="studentService" ref="studentService"></property>
<property name="sysuserService" ref="sysuserService"></property>
<property name="scoreService" ref="scoreService"></property>
</bean>
<bean id="scoreDAO" class="com.aowin.dao.imp.ScoreDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="studentDAO" class="com.aowin.dao.imp.StudentDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="sysuserDAO" class="com.aowin.dao.imp.SysuserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="scoreService" class="com.aowin.service.ScoreService">
<property name="scoreDAO" ref="scoreDAO"></property>
</bean>
<bean id="studentService" class="com.aowin.service.StudentService">
<property name="studentDAO" ref="studentDAO"></property>
</bean>
<bean id="sysuserService" class="com.aowin.service.SysuserService">
<property name="sysuserDAO" ref="sysuserDAO"></property>
</bean>
</beans>
下面是applicationContext
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<import resource="applicationContext_dao_service_action.xml"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="*add*">PROPAGATION_REQUIRED</prop>
<prop key="*delete*">PROPAGATION_REQUIRED</prop>
<prop key="*update*">PROPAGATION_REQUIRED</prop>
<prop key="*get*">readOnly</prop>
</props>
</property>
</bean>
<bean id="autoProxty" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Service"></property>
<property name="interceptorNames" value="transactionInterceptor"></property>
</bean>
</beans>
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 value
return 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 value
return mapping.findForward("fail");
}else{
request.setAttribute("username", username);
//老师信息的list
return 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");
}
}
}
//增加学生信息时做的操作的action
public 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 跳转到的时候直接跳到jsp
public 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");
}
}