struts+ibatis+spring整合开发


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>

Struts.xml
<?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">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="save" class="LoginAction" method="save">
<result name="success" type="redirect-action">show.action</result>
<result name="error">/error.jsp</result>
</action>
<action name="edit" class="LoginAction" method="edit">
<result name="success">update.jsp</result>
</action>
<action name="update" class="LoginAction" method="update">
<result name="success" type="redirect-action">show.action</result>
<result name="error">/error.jsp</result>
</action>
<action name="delete" class="LoginAction" method="delete">
<result name="success" type="redirect-action">show.action</result>
<result name="error">/error.jsp</result>
</action>
<action name="show" class="LoginAction" method="findAllUser">
<result name="success">/list.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>


Struts.properties

#struts.url.http.port=8080
struts.devMode=true
struts.configuration.xml.reload=true
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
struts.objectFactory=spring

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byType"
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://127.0.0.1:3306/debug" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>

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

<!-- bean中的class为实现接口的类;property的name为类中引用的属性名称;ref为spring注册的名称,如上面的client-->
<bean id="userDao"
class="cn.hsw.dao.UserDAO">
<property name="client" ref="client" />
</bean>

<bean id="userService"
class="cn.hsw.service.UserService">
<property name="userDAO" ref="userDao"></property>
</bean>

<bean id="LoginAction" class="cn.hsw.action.Login">
<property name="userService" ref="userService"></property>
</bean>
</beans>

package cn.hsw.action;

import java.util.List;

import cn.hsw.model.User;
import cn.hsw.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 IUserService userService=null;
private String id;
private int pageIndex=1;
private String pageBar;
private List<User> list;
private User user;

public void prepare()throws Exception{
if(id==null||id.length()==0){
user=new User();
}else{
user=getUserService().getUserById(Integer.parseInt(id));
}
}
public String execute()throws Exception{
if(getUserService().isLogin(user)){
return SUCCESS;
}
return INPUT;
}
public String save()throws Exception{
if(getUserService().insertUser(user)){
return SUCCESS;
}
return ERROR;
}
public String edit(){
return SUCCESS;
}
public String update()throws Exception{
if(getUserService().updateUser(user)){
return SUCCESS;
}
return ERROR;
}
public String delete()throws Exception{
if(getUserService().deleteUser(Integer.parseInt(id))){
return SUCCESS;
}
return ERROR;
}
public String findAllUser()throws Exception{
try {
list=getUserService().getAllUser();
} catch (Exception e) {
}
return SUCCESS;
}
public String getId() {
return id;
}

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

public int getPageIndex() {
return pageIndex;
}

public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}

public String getPageBar() {
return pageBar;
}

public void setPageBar(String pageBar) {
this.pageBar = pageBar;
}

public List<User> getList() {
return list;
}

public void setList(List<User> list) {
this.list = list;
}

public User getUser() {
return user;
}

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

public void setUserService(IUserService userService) {
this.userService = userService;
}
public IUserService getUserService() {
return userService;
}
public User getModel() {
// TODO Auto-generated method stub
return user;
}

}

IUserDAO.java

package cn.hsw.dao;

import java.util.List;

import cn.hsw.model.User;

public interface IUserDAO {
public List<User> getAllUser();
public User getUserById(Integer id);
public boolean isLogin(User user);
public boolean insertUser(User user);
public boolean updateUser(User user);
public boolean deleteUser(Integer id);
}

package cn.hsw.dao;

import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.hsw.model.User;

import com.ibatis.sqlmap.client.SqlMapClient;

public class UserDAO implements IUserDAO {

private SqlMapClient client = null;

public boolean deleteUser(Integer id) {
try {
client.delete("deleteUser", id);
return true;
} catch (Exception e) {
return false;
}
}

public List<User> getAllUser() {
List<User> list=null;
try{
list=client.queryForList("getAllUser");
}catch(Exception e){
e.getStackTrace();
}
return list;
}

public User getUserById(Integer id) {
User user=null;
try {
user=(User) client.queryForObject("getUserById",id);
return user;
} catch (Exception e) {
e.getStackTrace();
}
return user;
}

public boolean insertUser(User user) {
try{
client.insert("insertUser",user);
return true;
}catch(Exception e){
return false;
}
}

public boolean isLogin(User user) {
try {
User u=(User) client.queryForObject("checkUser",user);
if(u!=null){
return true;
}
} catch (Exception e) {
e.getStackTrace();
}
return false;
}

public boolean updateUser(User user) {
try {
client.update("updateUser",user);
return true;
} catch (Exception e) {
return false;
}
}

public void setClient(SqlMapClient client) {
this.client = client;
}

}

package cn.hsw.model;

public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

user.xml

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

<sqlMap namespace="User">
<typeAlias alias="user" type="cn.hsw.model.User"/>

<!-- 查询表中记录条数 -->
<select id="recordCount" resultClass="int">
<![CDATA[
select count(*) from user
]]>
</select>

<select id="getUserById" parameterClass="int" resultClass="user">
<![CDATA[
select * from user where id =#id#
]]>
</select>

<select id="getAllUser" resultClass="user">
<![CDATA[
select * from user
]]>
</select>

<select id="checkUser" parameterClass="user" resultClass="user">
<![CDATA[
select * from user where username =#username# and password =#password#
]]>
</select>
<!-- -->
<select id="firstPage" resultClass="user" parameterClass="user">
<![CDATA[
select top $pagesize$ * from user order by id desc
]]>
</select>

<delete id="deleteUser" parameterClass="int">
<![CDATA[
delete from user where id= #id#
]]>
</delete>

<update id="updateUser" parameterClass="user">
<![CDATA[
update user set username= #username#,password= #password# where id =#id#
]]>
</update>

<insert id="insertUser" parameterClass="user">
<![CDATA[
insert into user(username,password) values(#username#,#password#)
]]>
</insert>
</sqlMap>

IUserService.java

package cn.hsw.service;

import java.util.List;

import cn.hsw.model.User;

public interface IUserService {
public List<User> getAllUser();

public User getUserById(Integer id);

public boolean isLogin(User user);

public boolean insertUser(User user);

public boolean updateUser(User user);

public boolean deleteUser(Integer id);
}

UserService.java

package cn.hsw.service;

import java.util.List;

import cn.hsw.dao.IUserDAO;
import cn.hsw.dao.UserDAO;
import cn.hsw.model.User;

public class UserService implements IUserService {

private IUserDAO userDAO=null;

public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}

public boolean deleteUser(Integer id) {
return userDAO.deleteUser(id);
}

public List<User> getAllUser() {
return userDAO.getAllUser();
}

public User getUserById(Integer id) {
return userDAO.getUserById(id);
}

public boolean insertUser(User user) {
return userDAO.insertUser(user);
}

public boolean isLogin(User user) {
return userDAO.isLogin(user);
}

public boolean updateUser(User user) {
return userDAO.updateUser(user);
}

}

4. 页面

adduser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<title>My JSP 'adduser.jsp' starting page</title>

</head>

<body>
<s:form action="save" method="post">
<s:textfield label="username" name="username"/>
<s:password label="password" name="password"/>
<s:submit value="save"/>
</s:form>
</body>
</html>

error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>My JSP 'error.jsp' starting page</title>

</head>

<body>
This is my JSP page. <br>
</body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<title>My JSP 'list.jsp' starting page</title>
</head>

<body>
<s:form action="show" method="post">
<table width="50%" align="center">
<tr>
<td align="center">userid</td>
<td align="center">username</td>
<td align="center">password</td>
</tr>
<s:iterator value="list" id="user" status="st">
<tr>
<td align="center"><s:property value="id"/></td>
<td align="center"><s:property value="username"/></td>
<td align="center"><s:property value="password"/></td>
<td align="center">
<s:url id="update" action="edit">
<s:param name="id">
<s:property value="id"/>
</s:param>
</s:url>
<s:a href="%{update}">update</s:a>
</td>
<td align="center">
<s:url id="delete" action="delete">
<s:param name="id">
<s:property value="id" />
</s:param>
</s:url>
<s:a href="%{delete}">delete</s:a>
</td>
</tr>
</s:iterator>
<tr>
<td colspan="4">
<s:property value="#request.pageBar" escape="false"/>
</td>
</tr>
</table>
</s:form>
</body>
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

<html>
<head>

<title>My JSP 'login.jsp' starting page</title>

</head>

<body>
<s:form action="login" method="post">
<s:textfield label="username" name="username"/>
<s:password label="password" name="password"/>
<s:submit value="submit"/>
</s:form>
</body>
</html>

success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<title>success</title>
</head>

<body>
<a href="show.action">显示用户列表</a></br>
<a href="adduser.jsp">添加用户</a>
</body>
</html>

update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>

<title>update.jsp</title>
</head>

<body>
<s:form action="update" method="post">
<s:textfield name="id" label="ID" value="%{id}" readonly="true"/>
<s:textfield name="username" label="UserName" value="%{username}" required="true"/>
<s:textfield name="password" label="Password" value="%{password}" required="true"/>
<s:submit value="update"/>
</s:form>
</body>
</html>


表user
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL default '',
`password` varchar(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值