index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@include file="/common/s.jsp"%>
<%@include file="/common/meta.jsp"%>
<title>首页</title>
</head>
<body class="login">
<div class="box">
<div class="content">
<form id="login_form" class="form-horizontal" role="form" action="query/queryAll" style="width:320px;margin:0px auto;" method="post">
<div class="form-group">
<div class="col-md-10">
<button id="login" type="submit" class="btn btn-lg btn-primary btn-block">查询数据</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
leftrightTable.jsp
<%@page import="com.asiainfo.lcims.bean.PageBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.asiainfo.lcims.po.MAdmin"%>
<%@page import="java.util.List"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@include file="/taglibs.jsp"%>
<html>
<head>
<title>bootstrap表格数据左右轮播</title>
<%@include file="/common/s.jsp"%>
<%@include file="/common/meta.jsp"%>
<script>
$(document).ready(function() {
$('#carousel-ad').carousel({
interval : 3000
});
// 初始化轮播
$(".start-slide").click(function(){
$("#myCarousel").carousel('cycle');
});
// 停止轮播
$(".pause-slide").click(function(){
$("#myCarousel").carousel('pause');
});
// 循环轮播到上一个项目
$(".prev-slide").click(function(){
$("#myCarousel").carousel('prev');
});
// 循环轮播到下一个项目
$(".next-slide").click(function(){
$("#myCarousel").carousel('next');
});
});
</script>
</head>
<body>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<%! List<MAdmin> dataList = new ArrayList<MAdmin>();
int totalRows = 1;//总行数
int avgRows = 10;//每页行数
int totalPage = 1;
List<MAdmin> pageList = null;
String item = "";%>
<%
dataList = (List<MAdmin>) request.getAttribute("dataList");
totalRows = dataList.size();
totalPage = totalRows / avgRows + (totalRows % avgRows > 0 ? 1 : 0);
%>
总记录数:<%=totalRows%>
总页数:<%=totalPage%>
<%
for (int i = 1; i <= totalPage; i++) {
PageBean pb = new PageBean(i, avgRows, dataList);
pageList = pb.getPagerList();
if (i == 1) {
item = "item active";
} else {
item = "item";
}
%>
<div class="<%=item%>">
<div>
<table class="table table-bordered">
<thead>
<tr>
<th style="width: 100px;">编号</th>
<th style="width: 100px;">用户名</th>
<th style="width: 150px;">状态</th>
</tr>
</thead>
<tbody>
<c:forEach items="<%=pageList%>" var="s" varStatus="i">
<tr>
<td>${i.count}</td>
<td>${s.admin}</td>
<td>${s.status}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="center">第[<%=i%>]页</div>
<!-- 轮播(Carousel)导航 -->
<a class="carousel-control left prev-slide" href="#" data-slide="prev">‹</a>
<a class="carousel-control right next-slide" href="#" data-slide="next">›</a>
<!-- 控制按钮 -->
<div style="text-align:center;">
<input type="button" class="btn btn-primary start-slide" value="开始">
<input type="button" class="btn btn-primary pause-slide" value="暂停">
<input type="button" class="btn btn-primary prev-slide" value="前一项">
<input type="button" class="btn btn-primary next-slide" value="后一项">
</div>
</div>
<%
}
%>
</div>
</div>
</body>
</html>
springMVC controller:
LoginAction.java
package com.asiainfo.lcims.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.asiainfo.lcims.bo.MAdminBO;
import com.asiainfo.lcims.po.MAdmin;
@Controller
@RequestMapping("/query")
public class LoginAction {
private static final Logger log = LoggerFactory.getLogger(LoginAction.class);
@Resource(name = "mAdminBO")
private MAdminBO mAdminBO;
/**
* 信息查询
*
* @author zhaigx
*/
@RequestMapping("/queryAll")
public String queryAll(HttpServletRequest request, Model model) {
log.info("start query....");
String url = null;
try {
List<MAdmin> ret = mAdminBO.queryAll();
if (ret != null) {
log.info("query size:{}", ret.size());
url = "/jsp/leftrightTable.jsp";
model.addAttribute("dataList", ret);
return url;
}
} catch (Exception e) {
log.error("", e);
}
return "/error.jsp";
}
}
BO:
package com.asiainfo.lcims.bo;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.asiainfo.lcims.mapper.MAdminDAO;
import com.asiainfo.lcims.po.MAdmin;
@Service("mAdminBO")
public class MAdminBO {
@Resource(name = "mAdminDAO")
private MAdminDAO mAdminDAO;
public List<MAdmin> queryAll() {
List<MAdmin> list = mAdminDAO.queryAll();
return list;
}
}
DAO:
package com.asiainfo.lcims.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.asiainfo.lcims.po.MAdmin;
@Repository(value = "mAdminDAO")
public interface MAdminDAO {
public List<MAdmin> queryAll();
}
PageBean:
package com.asiainfo.lcims.bean;
import java.util.ArrayList;
import java.util.List;
public class PageBean<T> {
/**
* 当前页码
*/
private int currentPage;
/**
* 总页数
*/
private int totalPage;
/**
* 总行数
*/
private int totalRows;
/**
* 每页显示条数
*/
private int avgRows = 5;
/**
* 原集合
*/
private List<T> list;
public PageBean(int currentPage, int avgRows, List<T> list) {
super();
this.currentPage = currentPage;
this.avgRows = avgRows;
this.list = list;
this.totalRows = list.size();
this.totalPage = (this.totalRows - 1) / this.avgRows + 1;
}
public List<T> getPagerList() {
List<T> newList = new ArrayList<T>();
for (int i = (currentPage - 1) * avgRows; i < totalRows && i < currentPage * avgRows; i++) {
newList.add(list.get(i));
}
return newList;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getAvgRows() {
return avgRows;
}
public void setAvgRows(int avgRows) {
this.avgRows = avgRows;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
mybatis 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.asiainfo.lcims.mapper.MAdminDAO">
<select id="queryAll" resultType="M_ADMIN_ALIAS">
select * from M_ADMIN
</select>
</mapper>
applicationContext.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config />
<!-- 扫描service,dao组件 -->
<context:component-scan base-package="com.asiainfo.lcims.bo,com.asiainfo.lcims.mapper" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:17520:test" />
<property name="username" value="test123" />
<property name="password" value="test123" />
<property name="initialSize" value="0" />
<property name="maxActive" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="validationQuery">
<value>SELECT 1 from dual</value>
</property>
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-conf.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.asiainfo.lcims.mapper" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.Exception"
no-rollback-for="java.lang.RuntimeException" />
<tx:method name="rm*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.Exception"
no-rollback-for="java.lang.RuntimeException" />
<tx:method name="insert*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.RuntimeException" />
<tx:method name="add*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.RuntimeException" />
<tx:method name="save*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="mod*" propagation="REQUIRED"
read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="select*" propagation="SUPPORTS" />
<tx:method name="query*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!-- 把事务控制在Service层 -->
<aop:config>
<aop:pointcut id="pc"
expression="execution(* com.asiainfo.lcims.bo.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
</beans>
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<mvc:annotation-driven />
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.asiainfo.lcims.controller" >
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> -->
</context:component-scan>
<mvc:default-servlet-handler/>
<!-- <mvc:resources location="/css" mapping="/css/**" /> <mvc:resources
location="/images" mapping="/images/**" /> <mvc:resources location="/js"
mapping="/js/**" /> -->
<!-- Spring MVC JSON配置 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- json转换器 -->
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property>
</bean> -->
</beans>
mybatis-conf.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>
<settings>
<!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default: OTHER -->
<setting name="jdbcTypeForNull" value="NULL" />
</settings>
<typeAliases>
<typeAlias alias="M_ADMIN_ALIAS" type="com.asiainfo.lcims.po.MAdmin" />
</typeAliases>
<mappers>
<mapper resource="mapper/M_ADMIN.xml" />
</mappers>
</configuration>
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>
<!-- 配置 Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置springmvc -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>tex</extension>
<mime-type>application/x-tex</mime-type>
</mime-mapping>
<mime-mapping>
<extension>tar</extension>
<mime-type>application/x-tar</mime-type>
</mime-mapping>
<mime-mapping>
<extension>txt</extension>
<mime-type>application/octet-stream</mime-type>
</mime-mapping>
<mime-mapping>
<extension>gz</extension>
<mime-type>application/x-gzip</mime-type>
</mime-mapping>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.map</url-pattern>
</servlet-mapping>
</web-app>
启动类:
package com.asiainfo.lcims.server;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.asiainfo.lcims.config.SysConfig;
public class MainServer {
private static final Logger log = LoggerFactory.getLogger(MainServer.class);
public static void main(String[] args) {
SysConfig conf = new SysConfig();
try {
Server server = new Server();
int port = conf.getServerPort();
log.info("Server start : port:{}", port);
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(port);
connector.setMaxIdleTime(30000);
connector.setRequestHeaderSize(8192);
QueuedThreadPool threadPool = new QueuedThreadPool(20);
threadPool.setName("embed-jetty-http");
connector.setThreadPool(threadPool);
server.setConnectors(new Connector[] { connector });
WebAppContext ctx = new WebAppContext();
ctx.setContextPath("/");
ctx.setResourceBase(conf.getResourceBase());
server.setHandler(ctx);
server.setStopAtShutdown(true);
server.start();
server.join();
} catch (Exception e) {
log.error("", e);
}
}
}
引入的js:
<%@page language="java" pageEncoding="UTF-8"%>
<link href="${ctx}/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/css/flexslider.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/css/lcims.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${ctx}/js/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="${ctx}/js/bootstrap.min.js"></script>