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
分享到:
相关推荐
总的来说,Spring、Struts2和iBatis的整合为Java Web开发提供了一个强大、灵活的解决方案,让开发者能够更专注于业务逻辑,而不是框架的底层实现。通过合理的配置和使用这个jar包,开发者可以快速构建出稳定、高性能...
在Spring+Struts+ibatis这种经典的Java Web开发框架组合中,主要涉及到三层架构:表现层(Action)、业务逻辑层(Service)和数据访问层(DAO)。这些组件协同工作,实现了应用程序的功能。以下是对各部分的详细解释...
Struts2、Spring和iBatis是Java Web开发中三个非常重要的开源框架,它们共同构建了一个灵活、可扩展且高效的应用程序开发环境。这个“struts2+spring+iBatis框架包”集成了这三个框架,使得开发者能够快速构建基于...
标题中的"spring+struts+ibatis"是指一种经典的Java Web开发架构,也被称为SSM框架。这个架构结合了Spring框架、Struts2框架和iBatis(现在称为MyBatis)来构建高效且可维护的Web应用。下面将详细阐述这三个框架以及...
Struts2、Spring3和iBATIS是Java Web开发中常用的三大框架,它们各自负责不同的职责,协同工作可以构建出高效、松耦合的Web应用。在这个“struts2+spring3+ibatis项目整合案例”中,我们将深入探讨这三个框架如何...
### Struts+Spring+Ibatis整合框架搭建配置详解 在当今复杂的软件开发环境中,整合不同的框架以构建高效、可维护的应用程序变得尤为重要。Struts、Spring与Ibatis(现称MyBatis)作为三个功能互补的Java框架,它们...
通过这种方式,开发人员可以利用Struts2的控制层、Spring的服务层管理和依赖注入,以及Hibernate或Ibatis的数据访问能力,构建出高效且可扩展的Web应用。这些整合示例可以帮助初学者快速理解各个组件的交互方式,...
在《struts2+ibatis+spring整合开发》文档中,详细介绍了如何将Struts2、Ibatis与Spring三大框架进行整合,以实现一个高效、可扩展的企业级应用系统。本文将对这份文档的主要内容进行深度解析,并从中提炼出重要的...
Struts、Spring 和 iBatis 是 Java Web 开发中三个非常重要的开源框架,它们共同构建了一个灵活、可扩展且易于维护的系统架构。这个"Struts+Spring+Ibatis示例"提供了一个基础的整合应用,帮助开发者理解这三者如何...
Struts2+iBATIS+Spring整合是Java Web开发中一种常见的技术栈组合,这三种框架协同工作,可以构建出高效、灵活的企业级应用。Struts2作为MVC(Model-View-Controller)架构的一部分,主要负责处理HTTP请求,管理前端...
Struts、iBATIS和Spring是Java开发中常用的三大框架,它们各自负责不同的职责,而将它们整合在一起,可以构建出高效、灵活的企业级应用程序。本文将深入探讨这三者的核心概念、整合过程以及实际应用。 **Struts** ...
compass+ibatis+spring+struts2整合开发compass+ibatis+spring+struts2整合开发compass+ibatis+spring+struts2整合开发compass+ibatis+spring+struts2整合开发
struts2+ibatis+spring+Hessian 整合项目 web项目整合,服务端用hessian协议远程调用服务端的方法,hessian是用spring代理整合,struts2+ibatis+spring的整合项目,用作学习和开发基础平台构建很有用处,工程导入...
Struts、Spring和iBatis是Java开发中常用的三大开源框架,它们各自负责应用程序的不同层面,共同构建了一个灵活且强大的企业级应用开发解决方案。这里,我们深入探讨这三个框架以及它们如何协同工作。 **Struts框架...
Struts、Spring、Ibatis和Mysql是Java Web开发中常用的四大框架,它们组合在一起可以构建出高效、灵活的企业级应用程序。本示例是专为新手设计的一个整合教程,通过详细注解帮助初学者理解这四个组件如何协同工作。 ...
典型的Struts2+Ibatis+Spring整合项目的结构通常包括以下几个关键组成部分: - `src/main/webapp`: 存放Web相关的资源文件,如JSP页面、CSS样式表、JavaScript脚本等。 - `src/main/resources`: 存放配置文件,如`...
Struts2、Spring和iBatis是Java Web开发中常用的三大框架,它们分别负责MVC模式中的Action层、业务逻辑层和服务数据访问层。本文将详细介绍这三个框架如何整合,以及在实际项目中如何运用。 首先,Struts2作为表现...
这个属于与Spring整合的问题,Spring容器在初始化对象的时候会根据bean的scope配置来考虑是重新建立一个对象,还是继续使用原来的对象,拿struts2的action的bean来说,如果scope这个参数没有配置,默认就是单例,即...
**Spring、Struts2 和 iBatis 整合详解** 在Java Web开发中,Spring、Struts2 和 iBatis 是三个非常重要的框架。它们分别负责不同层面的任务:Spring 提供了全面的依赖注入(DI)和面向切面编程(AOP),用于管理...
这个"SSI"整合的Jar包,为开发者提供了一个快速搭建基于Struts2、Spring和Ibatis的开发环境,节省了手动下载和配置各个框架的时间,便于项目初始化和快速启动。开发者只需关注业务逻辑的实现,无需过多关注底层框架...