首页 > 代码库 > Struts2.3+Spring4.0
Struts2.3+Spring4.0
本例之通过Action调Service,Service掉Dao实现(主要掌握思想,注意Date的注入,以及javaBean的前台显示)
StudentAction-->StudentService-->StudentDao-->Student
Student.java
package cn.itcast.domain; import java.util.Date; public class Student { private Integer id; private String name; private String sex; private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + "]"; } }
StudentDao.java
package cn.itcast.dao; import cn.itcast.domain.Student; public class StudentDao { private Student student; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
StudentService.java
package cn.itcast.service; import cn.itcast.dao.StudentDao; import cn.itcast.domain.Student; public class StudentService { private StudentDao studentDao; public StudentDao getStudentDao() { return studentDao; } public void setStudentDao(StudentDao studentDao) { this.studentDao = studentDao; } public Student getStudent() { return studentDao.getStudent(); } }
StudentAction.java
package cn.itcast.web.action; import cn.itcast.domain.Student; import cn.itcast.service.StudentService; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { private static final long serialVersionUID = 1L; private StudentService studentService; public StudentService getStudentService() { return studentService; } public void setStudentService(StudentService studentService) { this.studentService = studentService; } @Override public String execute() throws Exception { Student student = studentService.getStudent(); System.out.println(student); ActionContext.getContext().getSession().put("student", student); return super.execute(); } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="http://www.mamicode.com/false" /> <constant name="struts.objectFactory" value="http://www.mamicode.com/spring" /> <package name="default" namespace="/" extends="struts-default"> <action name="spring" class="StudentAction"> <result name="success">/spring1.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
applicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 配置Action --> <bean id="StudentAction" class="cn.itcast.web.action.StudentAction" scope="prototype"> <property name="studentService" ref="studentService" /> </bean> <bean id="studentService" class="cn.itcast.service.StudentService" scope="prototype"> <property name="studentDao" ref="studentDao" /> </bean> <bean id="studentDao" class="cn.itcast.dao.StudentDao" scope="prototype"> <property name="student" ref="student" /> </bean> <bean id="dateFormat" class="java.text.SimpleDateFormat"> <constructor-arg value="http://www.mamicode.com/yyyy-MM-dd" /> </bean> <bean id="student" class="cn.itcast.domain.Student" scope="prototype"> <property name="id" value="http://www.mamicode.com/100" /> <property name="name" value="http://www.mamicode.com/陈新卫" /> <property name="sex" value="http://www.mamicode.com/男" /> <property name="birthday"> <bean factory-bean="dateFormat" factory-method="parse">
<!--注意此处通过dateFormat的parse方法把Date日期格式化,如果直接注入,而不经转换,则注入不成功,会保错。-->
<constructor-arg value="http://www.mamicode.com/1992-03-13" /> </bean>
</property>
</bean>
</beans>
spring1.jsp
<%@ page language="java" import="cn.itcast.domain.*" contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring1</title> </head> <body> <h1>Student Info by Spring!</h1> <table> <tr> <td colspan="2">Student Info</td> </tr> <tr> <td>id</td>
<!--注意前台显示bean属性的方法--> <td><input type="text" value="http://www.mamicode.com/${student.id}" /> </td> </tr> <tr> <td>name</td> <td><input type="text" value="http://www.mamicode.com/${student.name}" /> </td> </tr> <tr> <td>sex</td> <td><input type="text" value="http://www.mamicode.com/${student.sex}" /> </td> </tr> <tr> <td>sex</td> <td><input type="text" value="http://www.mamicode.com/${student.birthday}" /> </td> </tr> </table> </body> </html>
welcome.jsp
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加图书</title> </head> <body> <a href="http://www.mamicode.com/SS1/spring">显示</a> </body> </html>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。