标签:style blog class code java ext
本例之通过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="false" /> <constant name="struts.objectFactory" value="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="yyyy-MM-dd" /> </bean> <bean id="student" class="cn.itcast.domain.Student" scope="prototype"> <property name="id" value="100" /> <property name="name" value="陈新卫" /> <property name="sex" value="男" /> <property name="birthday"> <bean factory-bean="dateFormat" factory-method="parse">
<!--注意此处通过dateFormat的parse方法把Date日期格式化,如果直接注入,而不经转换,则注入不成功,会保错。-->
<constructor-arg value="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="${student.id}" /> </td> </tr> <tr> <td>name</td> <td><input type="text" value="${student.name}" /> </td> </tr> <tr> <td>sex</td> <td><input type="text" value="${student.sex}" /> </td> </tr> <tr> <td>sex</td> <td><input type="text" value="${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="/SS1/spring">显示</a> </body> </html>
Struts2.3+Spring4.0,布布扣,bubuko.com
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/jianfengyun/p/3719665.html