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="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>

时间: 2024-08-28 22:28:43

Struts2.3+Spring4.0的相关文章

struts-2.3+spring-4.0+hibernate-4.0整合项目

1.加入spring 1)加入jar包 2)配置web.xml applicationContext.xml (监听器) 3)加入spring的配置文件:aop context tx bean 1.加入hibernate 1.1建立持久化类,和其对应的xxx,hbm.xml文件,生产对应的数据表 1.2spring 整合hibernate 1)加入jar包 2)类路径下加入hibernate.cfg.xml文件.其中配置hibernate的基本配置 3)建立持久化类和对应的xxx.hbm.xml

spring4.0.6最新稳定版新特性学习,简单学习教程(一)

Spring Framework 4.0 学习整理. Spring框架的核心部分就是Ioc容器,而Ioc控制的就是各种Bean,一个Spring项目的水平往往从其XML配置文件内容就能略知一二,很多项目,往往是外包公司的项目,配置文件往往是乱七八糟,抱着能跑就行,不报错就行的态度去写,然后在项目中后期发现各种缺失又去一通乱补,其结果就是,整个文档可读性极差,毫无章法.这也不能怪写这个XML的人,拿着苦逼程序员的工资干着架构师的工作必然是这个结果.为了程序员的幸福,我认为有必要来一套简单快速的官方

[转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版本是: struts2.3.16.1 hibernate4.3.4 spring4.0.2 其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring http://repo.springsource.org/libs-rele

easyUI+Spring4.0.2+Struts2.3+Hibernate4.3.5 Demo

所用技术:easyUI.jQuery.Spring.Struts.Hibernate 参考网上的一篇Spring4.0.2+Struts2.3+Hibernate4.3.5整合的文章,前台使用easyUI和jQuery技术写的小例子. 工程可以在http://download.csdn.net/detail/fansy1990/7926009下载. 1. 工程配置修改: 1.1 修改configuration目录中db.properties中数据库配置:1.2 在数据库中运行sql目录下的sql

Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版本是: struts2.3.16.1 hibernate4.3.4 spring4.0.2 其中struts2和hibernate的下载方式比较简单,但是spring下载有点麻烦,可以直接复制下面链接下载最新版spring http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.2.RELEASE/spring-f

struts2.3.16 整合spring4.0.5 和 hibernate4.3.0

1.由struts2 框架自身根据struts.xml 中 的映射实例化Action 对象 Action 类代码如下: package com.hasonger.ssh.action; import java.util.Date; import com.hasonger.ssh.entity.User; import com.hasonger.ssh.service.UserService; import com.opensymphony.xwork2.ActionSupport; import

SpringMVC学习第一天(基于Spring4.0)

简介:Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口.在很多方面优秀于Struts2支持REST风格的URL请求采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性 简单HelloWorld例子(基于Spring4.0)1. 八个基础jar包- commons-logging-1.1.3.jar– spring-aop-4.

项目ITP(五) spring4.0 整合 Quartz 实现任务调度

前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了下,搞了个小样例给大家. 正文 spring4.0 整合 Quartz 实现任务调度.这是期末项目的最后一篇,剩下到暑假吧.  Quartz 介绍 Quartz is a full-featured, open source job scheduling service that can be in

SSH框架手动整合——Struts2+Hibernate4+Spring4

最近遇到过一些框架方面的问题,其中有MyBatis.SSH.SpringMVC,其中SSH的一些配置有些忘的差不多了,也有一些同事问了这些问题,前几个月也整合过SSH框架,那个时候是直接拿别人的Jar包直接整合框架,好像是Struts2+Hibernate3+Spring4,这次是相关的Jar从相关的官网下的. 我整合的环境: --Win 7 64 --MySQL 5.6 --MyEclipse 2014 --Jar包:struts-2.3.28.spring-framework-4.0.4.R