ssh集成
- ssh集成
spring:applicationContext.xml
datasource
sessionFactory
(加载数据源+方言+bean.hbm.xml)- TransactionManager 事务注解驱动
- bean(Action Service Dao)
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 先配置C3P0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///crm_28"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载方言 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载映射文件 -->
<property name="mappingResources">
<list>
<value>com/cqc/crm/domain/User.hbm.xml</value>
<value>com/cqc/crm/domain/Customer.hbm.xml</value>
<value>com/cqc/crm/domain/Dict.hbm.xml</value>
</list>
</property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启事务的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- customer模块 -->
<bean name="customerAction" class="com.cqc.crm.action.CustomerAction" scope="prototype">
<property name="customerService" ref="customerService"/>
</bean>
<bean name="customerService" class="com.cqc.crm.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<bean name="customerDao" class="com.cqc.crm.dao.impl.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
struts2: struts.xml
package>action>result
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 设置上传文件的总大小,默认是2M struts.multipart.maxSize=2097152 -->
<constant name="struts.multipart.maxSize" value="20971520"/>
<package name="crm" namespace="/" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="login">/login.jsp</result>
<result name="success">/index.jsp</result>
</action>
<action name="customer_*" class="customerAction" method="{1}">
<result name="list">/jsp/customer/list.jsp</result>
<result name="toList" type="redirectAction">customer_findByPage.action</result>
<result name="input" type="redirectAction">/jsp/error.jsp</result>
<!-- 引入默认的拦截器 -->
<interceptor-ref name="defaultStack">
<!-- 决定上传文件的类型 -->
<param name="fileUpload.allowedExtensions">.jpg,.txt</param>
</interceptor-ref>
</action>
<action name="dict_*" class="dictAction" method="{1}">
</action>
</package>
</struts>
hibernate:hibernate.cfg.xml(也可以由spring管理)
数据字典
实际项目中会把常量放到数据字典表中,根据key获取对应的value。如这个项目,把客户来源、客户级别、客户状态等放到字典表中。根据dict_type_code
查找对应的dict_item_name
Action *ServiceImpl *DaoIml
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
/**
*
*/
private static final long serialVersionUID = -9176812910048769004L;
private Customer customer = new Customer();
@Override
public Customer getModel() {
return customer;
}
}
@Transactional
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
@Override
public PageBean<Customer> findByPage(Integer pageCode, Integer pageSize, DetachedCriteria criteria) {
return customerDao.findByPage(pageCode,pageSize,criteria);
}
}
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
private static final long serialVersionUID = 3635798891795874167L;
@Override
public void add(Customer customer) {
Long serializable = (Long) getHibernateTemplate().save(customer);
}
}
注册
发送ajax请求
填写完用户名后要判断是否有重复,若重复,则提示“用户名不可用,重复”。
<INPUT id="user_code" style="WIDTH: 130px" name="user_code" onblur="checkCode()">
判断用户名是否已经存 先判断是否为空(发送ajax请求),为空则显示提示信息。
注意:ajax请求的写法
function checkCode(){
var user_code=$("#user_code").val();
if(user_code.trim()==""){
$("#RequiredFieldValidator3").html("请输入用户名");
$("#RequiredFieldValidator3").addClass("error");
}else{
//发送ajax请求判断
var url="${pageContext.request.contextPath}/user_checkUserName.action";
var params={"user_code":user_code};
$.post(url,params,function(data){
//如果有数据而且==yes,则用户名不重复
if(data && data == "yes"){
$("#RequiredFieldValidator3").html("");
$("#RequiredFieldValidator3").removeClass("error");
}else{
$("#RequiredFieldValidator3").html("用户名重复");
$("#RequiredFieldValidator3").addClass("error");
}
},"json");
}
}
后台返回json数据
public void checkUserName() {
String user_name = user.getUser_name();
List<User> list = userService.findUserByUserName(user_name);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter writer = null;
try {
writer = response.getWriter();
if (list != null && list.size() > 0) {
writer.print("no");
} else {
// 没有重复的,可以使用
writer.print("yes");
}
} catch (IOException e) {
e.printStackTrace();
}
}
##点击注册##
<FORM id=form1 name=form1 action="${pageContext.request.contextPath }/user_regist.action"
onsubmit="return checkForm()" method=post>
checkForm()返回false,就不会走action
登陆和退出登陆
登陆:把user保存到session中
ServletActionContext.getRequest().getSession().setAttribute("existUser", list.get(0));
退出登陆:把user从session中清空
ServletActionContext.getRequest().getSession().removeAttribute("existUser");
分页查询
分页-后端代码
action的编写
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private static final long serialVersionUID = -9176812910048769004L;
private Customer customer = new Customer();
@Override
public Customer getModel() {
return customer;
}
//设置默认值 当前页码
private Integer pageCode=1;//get()是才有效
public void setPageCode(Integer pageCode) {
if(pageCode==null) {
pageCode=1;
}
this.pageCode = pageCode;
}
//一页显示多少条数据
private Integer pageSize=2;
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
/**
* 分页查找,调转到list.jsp
* @return
*/
public String findByPage() {
DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
...
PageBean<Customer> pageBean=customerService.findByPage(pageCode,pageSize,criteria);
ValueStack vs = ActionContext.getContext().getValueStack();
vs.push(pageBean);
return "list";
}
}
分页dao的编写
@Override
public PageBean<Customer> findByPage(Integer pageCode, Integer pageSize, DetachedCriteria criteria) {
PageBean<Customer> page = new PageBean<>();
page.setPageCode(pageCode);
page.setPageSize(pageSize);
criteria.setProjection(Projections.rowCount());
List<Number> customerList = (List<Number>) getHibernateTemplate().findByCriteria(criteria);
if(customerList!=null && customerList.size()>0) {
page.setTotalCount(customerList.get(0).intValue());
criteria.setProjection(null);
}
List<Customer> list = (List<Customer>) getHibernateTemplate().findByCriteria(criteria, (pageCode-1)*pageSize, pageSize);
page.setBeanList(list);
return page;
}
PageBean.java
public class PageBean<T> {
// 当前页
private int pageCode;
// 总页数
// private int totalPage;
// 总记录数
private int totalCount;
// 每页显示的记录条数
private int pageSize;
// 每页显示的数据
private List<T> beanList;
public int getPageCode() {
return pageCode;
}
public void setPageCode(int pageCode) {
this.pageCode = pageCode;
}
/**
* 调用getTotalPage() 获取到总页数
* JavaBean的属性规定:totalPage是JavaBean是属性 ${pageBean.totalPage}
* @return
*/
public int getTotalPage() {
// 计算
int totalPage = totalCount / pageSize;
// 说明整除
if(totalCount % pageSize == 0){
return totalPage;
}else{
return totalPage + 1;
}
}
/*public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}*/
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<T> getBeanList() {
return beanList;
}
public void setBeanList(List<T> beanList) {
this.beanList = beanList;
}
}
分页-前端jsp
<DIV style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">
共[<B>${totalCount}</B>]条记录,[<B>${totalPage}</B>]页 ,每页显示
<select name="pageSize">
<option value="2"
<c:if test="${pageSize==2 }">selected</c:if>>2
</option>
<option value="3"
<c:if test="${pageSize==3 }">selected</c:if>>3
</option>
</select> 条
<c:if test="${pageCode>1 }">
[<A href="javascript:to_page(${pageCode-1})">前一页</A>] <B>${pageCode}</B>
</c:if>
<c:if test="${pageCode<totalPage }">
[<A href="javascript:to_page(${pageCode+1})">后一页</A>]
</c:if>
到 <input
type="text" size="3" id="page" name="pageCode" /> 页 <input
type="button" value="Go" onclick="to_page()" />
</DIV>
// 提交分页的查询的表单
function to_page(page){
if(page){
$("#page").val(page);
}
document.customerForm.submit();
}`
页面展示:
源码:
https://gitee.com/ssh_jicheng/cqc_crm28