spring+ibatis+struts的web项目

[size=medium][b]最近复习一下spring和ibatis的相关知识,做个简单的项目实践一下[/b]
[/size]
[b]准备工作:[/b]
myeclipse7.5+tomcat 6.0+MySQL
所需要的jar包:
[img]http://dl.iteye.com/upload/attachment/419878/d1e694bd-4315-3106-963f-1d8775557e44.jpg[/img]
[size=medium]下面是项目的实现:[/size]
[b]MySQL:[/b]
数据库名:xxx,表名:user
列名和字段类型:
[img]http://dl.iteye.com/upload/attachment/419880/a058ed8c-4930-34fd-8dd5-a899eaa35e75.jpg[/img]
[b]项目结构:[/b]
[img]http://dl.iteye.com/upload/attachment/419884/98ecf1fb-a4db-3acc-b0a9-301d813bfd33.jpg[/img]
[b]web.xml配置:[/b]

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- spring的启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts的启动 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

[b]applicationContext.xml的配置:[/b]

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
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" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xxx" />
<property name="username" value="root" />
<property name="password" value="kyo" />
</bean>

<bean id="client" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation" value="classpath:com/kyo/resources/sqlmap-config.xml">
</property>
</bean>

<bean id="userDAO" class="com.kyo.DAO.IbatisUserDAO">
<property name="client" ref="client"></property>
</bean>

<bean id="userService" class="com.kyo.service.UserService">
<property name="userDAO" ref="userDAO"></property>
</bean>

<bean id="LoginAction" class="com.kyo.actions.Login">
<property name="userService" ref="userService"></property>
</bean>
</beans>

[b]sqlmap-config.xml的配置:[/b]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/kyo/resources/user.xml"/>
</sqlMapConfig>

[b]action中Login类的设置:[/b]

package com.kyo.actions;

import java.util.List;

import com.kyo.model.User;
import com.kyo.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class Login extends ActionSupport implements ModelDriven<User>,
Preparable {

private String id;
private User user;
private List<User> users;
private IUserService userService;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}

public void setUserService(IUserService userService) {
this.userService = userService;
}

public void prepare() throws Exception {
if (id == null || id.length() == 0)
user = new User();
else
user = userService.getUserById(Integer.parseInt(id));
}

public User getModel() {
return user;
}

public String execute() {
if (userService.isLogin(user))
return SUCCESS;
return INPUT;
}

public String findAllUsers() {
users = userService.getAllUsers();
if (users.size() != 0)
{
return SUCCESS;
}

else
return ERROR;
}

}

[b]struts.xml的配置:[/b]

<?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>
<constant name="struts.objectFactory.spring.autoWire" value="type" />
<constant name="struts.objectFactory" value="spring" />
<include file="struts-default.xml"/>
<package name="struts2" extends="struts-default">
<default-interceptor-ref name="paramsPrepareParamsStack" />
<!-- aciton的class为applicationContext.xml中的注册名 -->
<action name="login" class="LoginAction" method="execute">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="show" class="LoginAction" method="findAllUsers">
<result name="success">/list.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

[b]其它的诸如model层、DAO层、service层大同小异,不一一赘述。[/b]


页面很简单,
登录页面:
[img]http://dl.iteye.com/upload/attachment/419892/a5dd8ff4-ae9d-3b9c-95e2-4a5f2a31c556.jpg[/img]
展示数据页面:
[img]http://dl.iteye.com/upload/attachment/419894/d7ebad0f-fee5-35c3-8510-aaebd5ba3e0d.jpg[/img]

[b][size=medium]评论:这次实践中碰到很多异常,查阅资料中,也碰到很多相关性的问题。[/size][/b]
比如:
1.Ibatis中的配置文件sqlmap-config.xml和user.xml头部很容易出错,需注意
sqlmap-config.xml

<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

user.xml

<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

如果出错,会报出
[quote]
Caused by: org.xml.sax.SAXParseException: Document root element "sqlMap", must match DOCTYPE root "sqlMapConfig".
[/quote]
2.Ibatis中client配置属性路径问题

<property name="configLocation" value="classpath:com/kyo/resources/sqlmap-config.xml">
</property>


3.Ibatis关键点,必须配置[color=red]org.springframework.orm.ibatis.SqlMapClientFactoryBea[/color]n,否则会出现空指针错误,ibatis不能起作用

4.另外很多问题都是由于jar包引用冲突,缺失jar包等问题,碰到这些问题需要小心排查,实在不行,百度知道,你就知道。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值