ssm环境搭建
Posted liushisaonian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm环境搭建相关的知识,希望对你有一定的参考价值。
基本环境搭建环境
1.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>boot-crm1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <!-- 配置监听器加载spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置SpringMVC --> <servlet> <servlet-name>boot-crm1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <!-- 配置springmvc什么时候启动,参数必须为整数 --> <!-- 如果为0或者大于0,则springMVC随着容器启动而启动 --> <!-- 如果小于0,则在第一次请求进来的时候启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>boot-crm1</servlet-name> <!-- 所有的请求都进入springMVC --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
2.WebContent下WEN-INF设置
3.配置文件
application-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置 读取properties文件 jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <property name="typeAliasesPackage" value="com.it.huyuan.crm.pojo" /> </bean> <!-- 动态代理dao扫描配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.it.huanyu.crm.mapper" /> </bean> </beans>
application-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置@Service注解包扫描 --> <context:component-scan base-package="com.it.huanyu.crm.service"/> </beans>
application-transxml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.it.huanyu.crm.service.*.*(..))" /> </aop:config> </beans>
application-controller.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置Controller扫描 --> <context:component-scan base-package="com.it.huanyu.crm.controller" /> <!-- 加载属性文件 --> <context:property-placeholder location="classpath:crm.properties"/> <!-- 配置注解驱动 --> <mvc:annotation-driven /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
crm.properties
#u5ba2u6237u6765u6e90u7f16u7801 CUSTOMER_FROM_TYPE=002 #u5ba2u6237u884cu4e1au7f16u7801 CUSTOMER_INDUSTRY_TYPE=001 #u5ba2u6237u7ea7u522bu7f16u7801 CUSTOMER_LEVEL_TYPE=006 //常亮的配置和引用 @Value("${CUSTOMER_FROM_TYPE}") private String CUSTOMER_FROM_TYPE;
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8 jdbc.username=root jdbc.password=123
log4j.properties
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
4.代码
Controller层
package com.it.huanyu.crm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.it.huanyu.crm.pojo.BaseDict; import com.it.huanyu.crm.pojo.Customer; import com.it.huanyu.crm.pojo.QueryVo; import com.it.huanyu.crm.service.BaseDictService; import com.it.huanyu.crm.service.CustomerService; import com.it.huanyu.crm.util.Page; @Controller @RequestMapping("customer") public class CustomerController { @Autowired private BaseDictService dictService; @Autowired private CustomerService customerService; @Value("${CUSTOMER_FROM_TYPE}") private String CUSTOMER_FROM_TYPE; //客户行业编码 @Value("${CUSTOMER_INDUSTRY_TYPE}") private String CUSTOMER_INDUSTRY_TYPE; //客户级别编码 @Value("${CUSTOMER_LEVEL_TYPE}") private String CUSTOMER_LEVEL_TYPE; @RequestMapping("list") public String list(Model model,QueryVo vo) { // 来源查询 List<BaseDict> fromType = dictService.getBaseDictByCode(CUSTOMER_FROM_TYPE); // 行业查询 List<BaseDict> industryType = dictService.getBaseDictByCode(CUSTOMER_INDUSTRY_TYPE); // 来源查询 List<BaseDict> levelType = dictService.getBaseDictByCode(CUSTOMER_LEVEL_TYPE); //分页查询用户列表 Page<Customer> page = customerService.getCustomerByQueryVo(vo); //设置数据模型返回 model.addAttribute("fromType", fromType); model.addAttribute("industryType", industryType); model.addAttribute("levelType", levelType); //设置分页数据返回 model.addAttribute("page", page); //查询条件回显 model.addAttribute("vo", vo); return "customer"; } @RequestMapping("edit") @ResponseBody public Customer edit(Integer id){ Customer customer = customerService.getCustomerById(id); return customer; } @RequestMapping("update") @ResponseBody public String update(Customer customer){ String msg = "0"; try { customerService.updateCustomer(customer); } catch (Exception e) { msg = "1"; e.printStackTrace(); } return msg; } @RequestMapping("delete") @ResponseBody public String delete(Integer id){ String msg = "0"; try { customerService.deleteCustomer(id); } catch (Exception e) { msg = "1"; e.printStackTrace(); } return msg; } @RequestMapping("myTag") public String myTag() { return "myTag"; } @RequestMapping("myTestTag") public String myTestTag(){ return "myTestTag"; } }
Service层
package com.it.huanyu.crm.service; import java.util.List; import com.it.huanyu.crm.pojo.BaseDict; /** * 字典持久化接口 * @author Steven * */ public interface BaseDictService { /** * 跟据编码查询字典列表 * @param code * @return */ List<BaseDict> getBaseDictByCode(String code); }
package com.it.huanyu.crm.service; import com.it.huanyu.crm.pojo.Customer; import com.it.huanyu.crm.pojo.QueryVo; import com.it.huanyu.crm.util.Page; public interface CustomerService { /** * 分页跟据查询条件查询客户列表 * @param vo * @return */ Page<Customer> getCustomerByQueryVo(QueryVo vo); /** * 跟据id查询客户信息 * @param id * @return */ Customer getCustomerById(Integer id); /** * 更新客户信息 * @param customer */ void updateCustomer(Customer customer); /** * 删除客户 * @param id */ void deleteCustomer(Integer id); }
package com.it.huanyu.crm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.it.huanyu.crm.mapper.BaseDictMapper; import com.it.huanyu.crm.pojo.BaseDict; import com.it.huanyu.crm.service.BaseDictService; @Service public class BaseDictServiceImpl implements BaseDictService { @Autowired private BaseDictMapper baseDictMapper; @Override public List<BaseDict> getBaseDictByCode(String code) { return baseDictMapper.getBaseDictByCode(code); } }
package com.it.huanyu.crm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.it.huanyu.crm.mapper.CustomerMapper; import com.it.huanyu.crm.pojo.Customer; import com.it.huanyu.crm.pojo.QueryVo; import com.it.huanyu.crm.service.CustomerService; import com.it.huanyu.crm.util.Page; @Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerMapper customerMapper; @Override public Page<Customer> getCustomerByQueryVo(QueryVo vo) { //计算数据库起始行数 vo.setStart((vo.getPage() - 1) * vo.getRows()); //查询用户列表 List<Customer> list = customerMapper.getCustomerByQueryVo(vo); //查询总记录数 Integer count = customerMapper.getCountByQueryVo(vo); //组装分页数据 Page<Customer> page = new Page<Customer>(count, vo.getPage(), vo.getRows(), list); return page; } @Override public Customer getCustomerById(Integer id) { return customerMapper.getCustomerById(id); } @Override public void updateCustomer(Customer customer) { customerMapper.updateCustomer(customer); } @Override public void deleteCustomer(Integer id) { customerMapper.deleteCustomer(id); } }
dao层
package com.it.huanyu.crm.mapper; import java.util.List; import com.it.huanyu.crm.pojo.BaseDict; /** * 字典持久化接口 * */ public interface BaseDictMapper { /** * 跟据编码查询字典列表 * @param code * @return */ List<BaseDict> getBaseDictByCode(String code); }
package com.it.huanyu.crm.mapper; import java.util.List; import com.it.huanyu.crm.pojo.Customer; import com.it.huanyu.crm.pojo.QueryVo; public interface CustomerMapper { /** * 跟据查询条件查询客户列表 * @param vo * @return */ List<Customer> getCustomerByQueryVo(QueryVo vo); /** * 跟据查询条件查询总记录数 * @param vo * @return */ Integer getCountByQueryVo(QueryVo vo); /** * 跟据id查询客户信息 * @param id * @return */ Customer getCustomerById(Integer id); /** * 更新客户信息 * @param customer */ void updateCustomer(Customer customer); /** * 删除客户 * @param id */ void deleteCustomer(Integer id); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.crm.mapper.BaseDictMapper"> <select id="getBaseDictByCode" parameterType="string" resultType="basedict"> SELECT `dict_id`, `dict_type_code`, `dict_type_name`, `dict_item_name`, `dict_item_code`, `dict_sort`, `dict_enable`, `dict_memo` FROM `base_dict` WHERE `dict_type_code` = #{code} </select> </mapper>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.crm.mapper.CustomerMapper"> <sql id="customer_where"> <where> <if test="custName != null and custName != ‘‘"> AND c.`cust_name` LIKE ‘%${custName }%‘ </if> <if test="custSource != null and custSource != ‘‘"> AND c.`cust_source` = #{custSource} </if> <if test="custIndustry != null and custIndustry != ‘‘"> AND c.`cust_industry` = #{custIndustry} </if> <if test="custLevel != null and custLevel != ‘‘"> AND c.`cust_level` = #{custLevel} </if> </where> </sql> <select id="getCustomerByQueryVo" parameterType="queryvo" resultType="customer"> SELECT c.`cust_id`, c.`cust_name`, c.`cust_user_id`, c.`cust_create_id`, s.`dict_item_name` `cust_source`, i.`dict_item_name` `cust_industry`, l.`dict_item_name` `cust_level`, c.`cust_linkman`, c.`cust_phone`, c.`cust_mobile`, c.`cust_zipcode`, c.`cust_address`, c.`cust_createtime` FROM `customer` c LEFT JOIN base_dict s ON s.`dict_id` = c.`cust_source` LEFT JOIN base_dict i ON i.`dict_id` = c.`cust_industry` LEFT JOIN base_dict l ON l.`dict_id` = c.`cust_level` <include refid="customer_where"/> <if test="start != null"> LIMIT #{start}, #{rows}; </if> </select> <select id="getCountByQueryVo" parameterType="queryvo" resultType="int"> SELECT count(1) FROM `customer` c LEFT JOIN base_dict s ON s.`dict_id` = c.`cust_source` LEFT JOIN base_dict i ON i.`dict_id` = c.`cust_industry` LEFT JOIN base_dict l ON l.`dict_id` = c.`cust_level` <include refid="customer_where"/> </select> <select id="getCustomerById" parameterType="int" resultType="customer"> SELECT `cust_id`, `cust_name`, `cust_user_id`, `cust_create_id`, `cust_source`, `cust_industry`, `cust_level`, `cust_linkman`, `cust_phone`, `cust_mobile`, `cust_zipcode`, `cust_address`, `cust_createtime` FROM `customer` WHERE cust_id = #{id} </select> <update id="updateCustomer" parameterType="customer"> UPDATE `customer` <set> <if test="cust_name!=null and cust_name!=‘‘"> `cust_name` = #{cust_name}, </if> <if test="cust_source!=null and cust_source!=‘‘"> `cust_source` = ‘#{cust_source}‘, </if> <if test="cust_industry!=null and cust_industry!=‘‘"> `cust_industry` = #{cust_industry}, </if> <if test="cust_level!=null and cust_level!=‘‘"> `cust_level` = #{cust_level}, </if> <if test="cust_linkman!=null and cust_linkman!=‘‘"> `cust_linkman` = #{cust_linkman}, </if> <if test="cust_phone!=null and cust_phone!=‘‘"> `cust_phone` = #{cust_phone}, </if> <if test="cust_mobile!=null and cust_mobile!=‘‘"> `cust_mobile` = #{cust_mobile}, </if> <if test="cust_zipcode!=null and cust_zipcode!=‘‘"> `cust_zipcode` = #{cust_zipcode}, </if> <if test="cust_address!=null and cust_address!=‘‘"> `cust_address` = #{cust_address}, </if> <if test="cust_createtime!=null "> `cust_createtime` = #{cust_createtime}, </if> </set> WHERE `cust_id` = #{cust_id}; </update> <delete id="deleteCustomer" parameterType="int"> DELETE FROM `customer` where `cust_id` = #{cust_id}; </delete> </mapper>
pojo
package com.it.huanyu.crm.pojo; /** * 字典数据模型 * */ public class BaseDict { private String dict_id; private String dict_type_code; private String dict_type_name; private String dict_item_name; private String dict_item_code; private Integer dict_sort; private String dict_enable; private String dict_memo; public String getDict_id() { return dict_id; } public void setDict_id(String dict_id) { this.dict_id = dict_id; } public String getDict_type_code() { return dict_type_code; } public void setDict_type_code(String dict_type_code) { this.dict_type_code = dict_type_code; } public String getDict_type_name() { return dict_type_name; } public void setDict_type_name(String dict_type_name) { this.dict_type_name = dict_type_name; } public String getDict_item_name() { return dict_item_name; } public void setDict_item_name(String dict_item_name) { this.dict_item_name = dict_item_name; } public String getDict_item_code() { return dict_item_code; } public void setDict_item_code(String dict_item_code) { this.dict_item_code = dict_item_code; } public Integer getDict_sort() { return dict_sort; } public void setDict_sort(Integer dict_sort) { this.dict_sort = dict_sort; } public String getDict_enable() { return dict_enable; } public void setDict_enable(String dict_enable) { this.dict_enable = dict_enable; } public String getDict_memo() { return dict_memo; } public void setDict_memo(String dict_memo) { this.dict_memo = dict_memo; } }
package com.it.huanyu.crm.pojo; import java.util.Date; /** * 客户信息数据模型 * */ public class Customer { private Long cust_id; private String cust_name; private Long cust_user_id; private Long cust_create_id; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; private String cust_zipcode; private String cust_address; private Date cust_createtime; public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public Long getCust_user_id() { return cust_user_id; } public void setCust_user_id(Long cust_user_id) { this.cust_user_id = cust_user_id; } public Long getCust_create_id() { return cust_create_id; } public void setCust_create_id(Long cust_create_id) { this.cust_create_id = cust_create_id; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } public String getCust_zipcode() { return cust_zipcode; } public void setCust_zipcode(String cust_zipcode) { this.cust_zipcode = cust_zipcode; } public String getCust_address() { return cust_address; } public void setCust_address(String cust_address) { this.cust_address = cust_address; } public Date getCust_createtime() { return cust_createtime; } public void setCust_createtime(Date cust_createtime) { this.cust_createtime = cust_createtime; } }
package com.it.huanyu.crm.pojo; public class QueryVo { private String custName; private String custSource; private String custIndustry; private String custLevel; // 当前页码数 private Integer page = 1; // 数据库从哪一条数据开始查 private Integer start; // 每页显示数据条数 private Integer rows = 10; public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public Integer getStart() { return start; } public void setStart(Integer start) { this.start = start; } public Integer getRows() { return rows; } public void setRows(Integer rows) { this.rows = rows; } }
utils
package com.it.huanyu.crm.util; import java.io.IOException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; /** * 显示格式 上一页 1 2 3 4 5 下一页 */ public class NavigationTag extends TagSupport { static final long serialVersionUID = 2372405317744358833L; /** * request 中用于保存Page<E> 对象的变量名,默认为“page” */ private String bean = "page"; /** * 分页跳转的url地址,此属性必须 */ private String url = null; /** * 显示页码数量 */ private int number = 5; @Override public int doStartTag() throws JspException { JspWriter writer = pageContext.getOut(); HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); Page page = (Page) request.getAttribute(bean); if (page == null) return SKIP_BODY; url = resolveUrl(url, pageContext); try { // 计算总页数 int pageCount = page.getTotal() / page.getSize(); if (page.getTotal() % page.getSize() > 0) { pageCount++; } writer.print("<nav><ul class="pagination">"); // 显示“上一页”按钮 if (page.getPage() > 1) { String preUrl = append(url, "page", page.getPage() - 1); preUrl = append(preUrl, "rows", page.getSize()); writer.print("<li><a href="" + preUrl + "">上一页</a></li>"); } else { writer.print("<li class="disabled"><a href="#">上一页</a></li>"); } // 显示当前页码的前2页码和后两页码 // 若1 则 1 2 3 4 5, 若2 则 1 2 3 4 5, 若3 则1 2 3 4 5, // 若4 则 2 3 4 5 6 ,若10 则 8 9 10 11 12 int indexPage = (page.getPage() - 2 > 0) ? page.getPage() - 2 : 1; for (int i = 1; i <= number && indexPage <= pageCount; indexPage++, i++) { if (indexPage == page.getPage()) { writer.print("<li class="active"><a href="#">" + indexPage + "<span class="sr-only">(current)</span></a></li>"); continue; } String pageUrl = append(url, "page", indexPage); pageUrl = append(pageUrl, "rows", page.getSize()); writer.print("<li><a href="" + pageUrl + "">" + indexPage + "</a></li>"); } // 显示“下一页”按钮 if (page.getPage() < pageCount) { String nextUrl = append(url, "page", page.getPage() + 1); nextUrl = append(nextUrl, "rows", page.getSize()); writer.print("<li><a href="" + nextUrl + "">下一页</a></li>"); } else { writer.print("<li class="disabled"><a href="#">下一页</a></li>"); } writer.print("</nav>"); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } private String append(String url, String key, int value) { return append(url, key, String.valueOf(value)); } /** * 为url 参加参数对儿 * * @param url * @param key * @param value * @return */ private String append(String url, String key, String value) { if (url == null || url.trim().length() == 0) { return ""; } if (url.indexOf("?") == -1) { url = url + "?" + key + "=" + value; } else { if (url.endsWith("?")) { url = url + key + "=" + value; } else { url = url + "&" + key + "=" + value; } } return url; } /** * 为url 添加翻页请求参数 * * @param url * @param pageContext * @return * @throws javax.servlet.jsp.JspException */ private String resolveUrl(String url, javax.servlet.jsp.PageContext pageContext) throws JspException { // UrlSupport.resolveUrl(url, context, pageContext) Map params = pageContext.getRequest().getParameterMap(); for (Object key : params.keySet()) { if ("page".equals(key) || "rows".equals(key)) continue; Object value = params.get(key); if (value == null) continue; try { if (value.getClass().isArray()) { // 解决GET乱码问题 // value = new String(((String[]) // value)[0].getBytes("ISO-8859-1"), "UTF-8"); value = ((String[]) value)[0]; url = append(url, key.toString(), value.toString()); } else if (value instanceof String) { // 解决GET乱码问题 // value = new String(((String) // value).getBytes("ISO-8859-1"), "UTF-8"); value = (String) value; url = append(url, key.toString(), value.toString()); } } catch (Exception e) { e.printStackTrace(); } } return url; } /** * @return the bean */ public String getBean() { return bean; } /** * @param bean * the bean to set */ public void setBean(String bean) { this.bean = bean; } /** * @return the url */ public String getUrl() { return url; } /** * @param url * the url to set */ public void setUrl(String url) { this.url = url; } public void setNumber(int number) { this.number = number; } }
package com.it.huanyu.crm.util; import java.util.List; public class Page<T> { private int total; private int page; private int size; private List<T> rows; public Page() { super(); } /** * * @param total * 查询数据总条数 * @param page * 当前页码数 * @param size * 每页显示数据条数 * @param rows * 查询结果集 */ public Page(int total, int page, int size, List<T> rows) { super(); this.total = total; this.page = page; this.size = size; this.rows = rows; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } }
以上是关于ssm环境搭建的主要内容,如果未能解决你的问题,请参考以下文章