當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
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;}@Overridepublic 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;}@Overridepublic 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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://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>??
轉載于:https://www.cnblogs.com/jianfengyun/p/3719665.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Struts2.3+Spring4.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java判断一个类是否公共类
- 下一篇: Inno setup