在网上看到好多个版本,自己有时间索性就写个Demo记录下整个框架的逻辑流程;
1、首先拷贝整个框架所需要的jar包到WEB-INF/lib包下(这个网上都可以搜到的)
2、配置文件的配置,
2.1、在web.xml配置文件中,是整个项目的入口配置文件,需要配置struts核心配置、Spring核心配置、监听spring配置
1
2
3 contextConfigLocation
4
5 /WEB-INF/spring/applicationContext-web.xml
6
7
8
9 class>org.springframework.web.context.ContextLoaderListenerclass>
10
11
12
13
14
15 struts2
16 class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterclass>
17
18
19
20 struts2
21 /*
22
2.2、将struts2的主配置文件复制到此项目中,可以复制到(classpath:)src的目录下即可
value="com.hsinghsu.testSSI.action" />
<package name="default" namespace="/" extends="struts-default">
register" class="com.test.action.RegisterAction"
method="addUser">
index.jsp
register.jsp
package>
2.3、Spring框架的搭建,创建applicationContext-web.xml配置文件,
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
/WEB-INF/ibatis/jdbc.properties
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
/WEB-INF/ibatis/SqlMapConfig.xml
class="com.test.daoImpl.UserDaoImpl">
class="com.test.serviceImpl.UserServiceImpl">
class="com.test.action.RegisterAction"
scope="prototype">
package="com.test" />
2.4、连接数据库的相关配置文件jdbc.properties和SqlMapConfig.xml配置文件(分别集中到一个文件夹中,)
jdbc.properties
jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=fdx
jdbc.username=sa
jdbc.password=abc123456
SqlMapConfig.xml【sql的映射文件的配置】
User.xml配置文件中放的具体的sql语句的操作:
insert into
[fdx].[dbo].[User]
(name,pass,username,address)values(#name#,#pass#,#username#,#address#)
以上都是配置文件的配置,接下来是部分java代码部分
dao接口
1 package com.test.dao;
2
3 import com.test.pojo.User;
4
5 public interface UserDao {
6 //用户注册
7 public boolean insertUser(User user);
8 }
daoImpl实现类
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.test.dao.UserDao;
import com.test.pojo.User;
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao{
@Override
public boolean insertUser(User user) {
// TODO Auto-generated method stub
try {
System.out.println("111111:"+user.getId()+user.getAddress());
getSqlMapClientTemplate().insert("insertUser", user);
//直接调用封装好的增删改查方法return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
}
service接口
package com.test.service;
import com.test.pojo.User;
public interface UserService {
//注册服务
public boolean insertUser(User user);
//根据name查看用户信息
public User queryUserByName(String name);
}
serviceImpl实现类【业务实现类】
package com.test.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.test.dao.UserDao;
import com.test.pojo.User;
import com.test.service.UserService;
public class UserServiceImpl implements UserService{
@Autowired//【自动注解的注入】
private UserDao userdao;
public UserDao getUserdao() {
return userdao;
}
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
@Override
public boolean insertUser(User user) {
// TODO Auto-generated method stub
System.out.println("进入到service实现类中"+user.getAddress()+":地址");
return userdao.insertUser(user);
}
}
RegisterAction类
public class RegisterAction extends ActionSupport implements ServletRequestAware{
/**
* 序列化
*/
private static final long serialVersiOnUID= 1L;
@Autowired
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
request = arg0;
}
public String addUser() {
System.out.println("进入action的注册功能----------------");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String username = request.getParameter("username");
String address = request.getParameter("address");
System.out.println("用户名"+name+"密码:"+pass+"昵称"+username+"地址:"+address);
User user = new User();
user.setName(name);
user.setPass(pass);
user.setUsername(username);
user.setAddress(address);
System.out.println("用户名"+user.getName()+"密码:"+user.getPass()+"昵称"+user.getUsername()+"地址:"+user.getAddress());
boolean b = userService.insertUser(user);
System.out.println(b+"执行了action");
if (b==true) {
return SUCCESS;
} else
return INPUT;
}
}
前台的jsp页面的访问
<body>
<form action="/SSITest/register" method="post">
用户名:<input type="text" name="name">br>
密 码:<input type="password" name="pass">br>
昵 称:<input type="text" name="username">br>
地 址:<input type="text" name="address">
<input type="submit" value="注册">
<input type="reset" value="取消">
form>
body>
然后按照MVC的模式设计Demo,然后测试Struts2框架,判断是否搭建成功,整个项目目录如下:
使用SSI框架写的简单Demo(注册模块)