码迷,mamicode.com
首页 > 其他好文 > 详细

使用SSI框架写的简单Demo(注册模块)

时间:2016-01-13 12:54:59      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

在网上看到好多个版本,自己有时间索性就写个Demo记录下整个框架的逻辑流程;

1、首先拷贝整个框架所需要的jar包到WEB-INF/lib包下(这个网上都可以搜到的)

2、配置文件的配置,

  2.1、在web.xml配置文件中,是整个项目的入口配置文件,需要配置struts核心配置、Spring核心配置、监听spring配置

 1 <!-- springframework config files -->
 2     <context-param>
 3         <param-name>contextConfigLocation</param-name>
 4         <!-- 把spring的配置文件放到了/WEB-INF/下的springframework包里,方便统一管理,命名规则是以applicationContent-开头的xml文件,初始化时会自动搜索所有符合规则的配置文件 -->
 5         <param-value>/WEB-INF/spring/applicationContext-web.xml</param-value>
 6     </context-param>
 7 
 8     <listener> <!--监听器 用于初始化spring框架 -->
 9         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10     </listener>
11 
12 
13     <!-- struts2模块 -->
14     <filter>
15         <filter-name>struts2</filter-name><!-- 这个就是struts2的核心过滤器 -->
16         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
17     </filter>
18 
19     <filter-mapping>
20         <filter-name>struts2</filter-name>
21         <url-pattern>/*</url-pattern>
22     </filter-mapping>

  2.2、将struts2的主配置文件复制到此项目中,可以复制到(classpath:)src的目录下即可

<struts>

    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.configuration.xml.reload" value="false" />
    <constant name="struts.custom.i18n.resources" value="globalMessages" />
   <constant name="struts.action.extension" value="action,," />
     <constant name="struts.convention.package.locators.basePackage"
     value="com.hsinghsu.testSSI.action" />
     <constant name="struts.convention.result.path" value="/" />
     <constant name="struts.serve.static" value="true" />
     <constant name="struts.serve.static.browserCache" value="false" />

    <package name="default" namespace="/" extends="struts-default">
    <!-- userAction -->
        <action name="register" class="com.test.action.RegisterAction"
            method="addUser">
            <result name="success">index.jsp</result> <!-- 注册成功 -->
            <result name="input">register.jsp</result><!-- 注册失败 -->
        </action>
    </package>
</struts>

  2.3、Spring框架的搭建,创建applicationContext-web.xml配置文件,  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    <!-- 配置数据源,连接池采用的是c3p0,具体各参数代表意义参看c3p0自带的doc, -->
<!--dataSource的相关配置  -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
</bean>
    <!-- 上面的数据源的value值用的是表达式,原因就在这里,这将配置文件放到了iBatis目录下,也就是jdbc.properties,设置了c3p0的各项参数 -->
    <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location"><value>/WEB-INF/ibatis/jdbc.properties</value></property>
    </bean>
    <!-- 配置iBatis的sqlMapClient,这里交给了spring去处理,其中SqlMapConfig文件放到了WEB-INF的iBatis目录下,也是便于管理 -->
    <!-- Spring整合ibatis配置文件,注入sqlMapClient -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation"><value>/WEB-INF/ibatis/SqlMapConfig.xml</value></property>
        <property name="dataSource"><ref bean="dataSource" /></property><!-- 这里使用的数据源就是上面配置的数据源 -->
    </bean>
    
    <!--user的相关操作配置  -->
    <bean id="userdaoImpl" class="com.test.daoImpl.UserDaoImpl">
        <property name="sqlMapClient" ref="sqlMapClient"></property>
    </bean>
    <bean id="userServiceImpl" name="userServiceImpl" class="com.test.serviceImpl.UserServiceImpl">
        <property name="userdao" ref="userdaoImpl">
        </property>
    </bean>
<!-- 用户注册action -->
    <bean id="registerAction" name="registerAction" class="com.test.action.RegisterAction"
        scope="prototype">
        <property name="userService" ref="userServiceImpl"></property>
    </bean> 
<!--  添加扫描、自动注解的配置-->
 <context:annotation-config /> 
    <context:component-scan base-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的映射文件的配置】

<?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/test/ibatis/sqlMap/User.xml" />
</sqlMapConfig>

User.xml配置文件中放的具体的sql语句的操作:

<sqlMap>
    <typeAlias alias="User" type="com.test.pojo.User" />
    <!-- 保存注册信息 -->
    <insert id="insertUser" parameterClass="User">
        insert into
        [fdx].[dbo].[User]
        (name,pass,username,address)values(#name#,#pass#,#username#,#address#)
    </insert>
  <select>......</select>
</sqlMap>

以上都是配置文件的配置,接下来是部分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(注册模块)

标签:

原文地址:http://www.cnblogs.com/FanSunny/p/5126787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!