📄 让第一个ssh2跑起来.htm
字号:
}
</PRE><BR>好了,数据持久层(dao)开发好了,按顺序,现在应该开发业务层了, <BR>新建包com.test.service
<BR>建类UserService.java <BR>代码: <BR><PRE class=java name="code">package com.test.service;
import java.util.List;
import com.test.bean.User;
public interface UserService
{
public List<User> findAll();
public void save(User user);
public void delete(User user);
public User findById(Integer id);
public void update(User user);
}
</PRE>这是一个接口类 <BR>然后写实现类 <BR>新建包 <BR>com.test.service.impl <BR>建类
<BR>UserServiceImpl.java <BR><PRE class=java name="code">package com.test.service.impl;
import java.util.List;
import com.test.bean.User;
import com.test.dao.UserDAO;
import com.test.service.UserService;
public class UserServiceImpl implements UserService {
private UserDAO userDao;
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
//服务层的方法
public void delete(User user)
{
this.userDao.removeUser(user);
}
public List<User> findAll() {
return this.userDao.findAllUsers();
}
public User findById(Integer id) {
return this.userDao.findUserById(id);
}
public void save(User user)
{
this.userDao.saveUser(user);
}
public void update(User user)
{
this.userDao.updateUser(user);
}
}
</PRE><BR><BR>业务和持久层都开发好了,现在我们回到控制层action <BR>修改action的代码如下: <BR><PRE class=java name="code">package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.*;
public class SaveUserAction extends ActionSupport {
private User user;
private UserService service;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception
{
this.service.save(this.user);
return SUCCESS;
}
}
</PRE><BR>修改struts.xml <BR>如下 <BR><PRE class=xml name="code"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="user" extends="struts-default">
<action name="saveUser" class="saveUserAction">
<result name="success" type="redirect">listUser.action</result>
<result name="input">/saveUser.jsp</result>
</action>
</package>
</struts>
</PRE><BR><BR>至此,我们已经开发好了一个典型的mvc项目,现在的任务是将strust2和hibernate都放入spring容器管理,
<BR>现在我们的项目结构如下: <BR><BR><IMG
src="让第一个ssh2跑起来.files/09b8cdc4-0d0c-37c4-9b47-4e9936c4f486.jpg">
<BR><BR><BR>然后修改 applicationContext.xml <BR>代码如下 注意注释 <BR><PRE class=xml name="code"><?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置数据源 -->
<!-- 一下分别配置了 数据库驱动 数据库路径 用户名 密码等 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- 一下配置sessionFactory 这个东西曾经是在hibernate中定义的 如今交给了spring管理 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 以下使一些用到的类 在使用类中均没有实例化这个类,都交给了spring管理 -->
<!-- DAObean -->
<bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- DAObean -->
<bean id="userService" class="com.test.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="saveUserAction" class="com.test.action.user.SaveUserAction">
<property name="service" ref="userService"></property>
</bean>
</beans>
</PRE><BR><BR><BR>好了 到此为止,ssh2的整合已经完成了,发布后开启服务器吧, <BR><BR>不对,服务器开启后有异常
<BR><BR><IMG src="让第一个ssh2跑起来.files/ca037e09-6f1d-35ea-a859-bbeeb0053f9e.jpg">
<BR>这里是包冲突问题 <BR>到项目的lib目录下, <BR><BR><IMG
src="让第一个ssh2跑起来.files/a514fd92-380b-3c5f-903d-3da0f2ea49c1.jpg"> <BR>删除选中的jar即可
<BR>ok了 可以测试了 <BR><A href="http://localhost:8080/ssh2-2/save.jsp"
target=_blank>http://localhost:8080/ssh2-2/save.jsp</A> </DIV>
<DIV class=clearfix id=digg_bottom><A onclick="digg_blog(366306);return false;"
href="http://radovi.javaeye.com/blog/366306#">
<DIV><STRONG>7</STRONG> <BR>顶</DIV></A><A
onclick="bury_blog(366306);return false;"
href="http://radovi.javaeye.com/blog/366306#">
<DIV><STRONG>2</STRONG> <BR>踩</DIV></A></DIV>
<DIV class=attachments>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93496/5b44b28b-0285-3bc4-a02f-ab43861134a4.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=5b44b28b-0285-3bc4-a02f-ab43861134a4-thumb
src="让第一个ssh2跑起来.files/5b44b28b-0285-3bc4-a02f-ab43861134a4-thumb.jpg"></A>
<LI>大小: 18 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93500/2906ef4c-28ca-350e-a534-b655eda059a8.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=2906ef4c-28ca-350e-a534-b655eda059a8-thumb
src="让第一个ssh2跑起来.files/2906ef4c-28ca-350e-a534-b655eda059a8-thumb.jpg"></A>
<LI>大小: 14.5 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93502/247dbe85-1e89-35f1-8b05-60df442d39ec.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=247dbe85-1e89-35f1-8b05-60df442d39ec-thumb
src="让第一个ssh2跑起来.files/247dbe85-1e89-35f1-8b05-60df442d39ec-thumb.jpg"></A>
<LI>大小: 42.1 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93510/f0e22b3a-196e-391a-bd1a-c1d4561330f5.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=F0e22b3a-196e-391a-bd1a-c1d4561330f5-thumb
src="让第一个ssh2跑起来.files/f0e22b3a-196e-391a-bd1a-c1d4561330f5-thumb.jpg"></A>
<LI>大小: 48.6 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93512/fe34b346-2165-33e2-9ede-753346c8b0ec.bmp"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=Fe34b346-2165-33e2-9ede-753346c8b0ec-thumb
src="让第一个ssh2跑起来.files/fe34b346-2165-33e2-9ede-753346c8b0ec-thumb.png"></A>
<LI>大小: 18.8 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93514/6a4570e8-1c02-3f2b-8a03-5fe409881380.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=6a4570e8-1c02-3f2b-8a03-5fe409881380-thumb
src="让第一个ssh2跑起来.files/6a4570e8-1c02-3f2b-8a03-5fe409881380-thumb.jpg"></A>
<LI>大小: 19.2 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93516/e3389717-3280-3e97-ab5f-e829ba060f87.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=E3389717-3280-3e97-ab5f-e829ba060f87-thumb
src="让第一个ssh2跑起来.files/e3389717-3280-3e97-ab5f-e829ba060f87-thumb.jpg"></A>
<LI>大小: 14.7 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93518/4d1f7daf-f9c7-3197-9cfe-1177a7f8a3b8.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=4d1f7daf-f9c7-3197-9cfe-1177a7f8a3b8-thumb
src="让第一个ssh2跑起来.files/4d1f7daf-f9c7-3197-9cfe-1177a7f8a3b8-thumb.jpg"></A>
<LI>大小: 30.6 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93520/433260c7-a3bb-3937-aa23-d38e72748f03.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=433260c7-a3bb-3937-aa23-d38e72748f03-thumb
src="让第一个ssh2跑起来.files/433260c7-a3bb-3937-aa23-d38e72748f03-thumb.jpg"></A>
<LI>大小: 60.2 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93549/09b8cdc4-0d0c-37c4-9b47-4e9936c4f486.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=09b8cdc4-0d0c-37c4-9b47-4e9936c4f486-thumb
src="让第一个ssh2跑起来.files/09b8cdc4-0d0c-37c4-9b47-4e9936c4f486-thumb.jpg"></A>
<LI>大小: 30.5 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93552/ca037e09-6f1d-35ea-a859-bbeeb0053f9e.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=Ca037e09-6f1d-35ea-a859-bbeeb0053f9e-thumb
src="让第一个ssh2跑起来.files/ca037e09-6f1d-35ea-a859-bbeeb0053f9e-thumb.jpg"></A>
<LI>大小: 33.7 KB </LI></UL>
<UL style="DISPLAY: none">
<LI><A
href="http://www.javaeye.com/upload/attachment/93554/a514fd92-380b-3c5f-903d-3da0f2ea49c1.jpg"
target=_blank><IMG class=magplus title=点击查看原始大小图片
alt=A514fd92-380b-3c5f-903d-3da0f2ea49c1-thumb
src="让第一个ssh2跑起来.files/a514fd92-380b-3c5f-903d-3da0f2ea49c1-thumb.jpg"></A>
<LI>大小: 91.1 KB </LI></UL>
<UL>
<LI><A
href="http://radovi.javaeye.com/topics/download/0ff8f2fa-6379-3286-89a0-f249b171678b">mysql-connector-java-5.0.8-bin.jar</A>
(528.2 KB)
<LI>下载次数: 18 </LI></UL>
<UL>
<LI><A
onclick="$$('div.attachments ul').invoke('show');$(this).up(1).hide();return false;"
href="http://radovi.javaeye.com/blog/366306#">查看图片附件</A> </LI></UL></DIV>
<DIV class=blog_bottom>
<UL>
<LI>00:46 </LI>
<LI>浏览 (338) </LI>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -