整体环境参见本人另一文:http://blog.csdn.net/jiangyu1013/article/details/51983360
此工程访问入口为index.jsp页面. 欢迎java朋友指导我的学习。个人QQ:283810473.
工程结构:
1. jap 页面:
index.jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>去确定页面</title> </head> <body> <a href="javascript:<jsp:forward page='WEB-INF/views/sure.jsp'/>"></a> </body> </html>
sure.jsp :
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>确定页面</title> <script type="text/javascript" src="js/jquery-1.12.0.min.js"></script> <script type="text/javascript" src="js/jquery.watermark.min.js"></script> <script language="javascript"> function doSubmit(){ $.ajax({ url:"<%=basePath%>sure/to", type:"post", data:$("#formMark").serialize(), success:function(data){ if(data){ window.location.href="<%=basePath%>sure/to"; }else{ alert(data); } }, error:function(){alert("有异常!");} }); } </script> </head> <body> <div> <div><span>确定页面</span></div> <form id="formMark"> <table> <tr> <td width="15%"> 你想知道book表有多少条数据吗? </td> </tr> </table> <div> <input type="button" style="margin-left:15px;" value="想" onclick="doSubmit()"/> </div> </form> </div> </body> </html>
result.jsp :
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%-- <% String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> --%> <%-- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> --%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>结果页面</title> </head> <body> <div> <div ><span>结果页面</span></div> <table > <tr> <td> book表数据总条数为:${count} 条。 </td> </tr> </table> </div> </body> </html>
stillResult.jsp :
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><% String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>结果页面</title> <base href="<%=basePath%>"> </head> <body> <div> <div ><span>结果页面</span></div> <table > <tr> <td> 这是still页面。 </td> </tr> </table> </div><pre name="code" class="html"></body></html>
2.代码部分:
SureController.java
package com.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.service.SureServiceImpl; @Controller @RequestMapping("/sure") public class SureController { @Resource private SureServiceImpl sureService; @RequestMapping("/to") public String getCount(ModelMap model) throws Exception { int count = sureService.getCount(); model.addAttribute("count",count); return "result"; } @RequestMapping(value="/still") public String still(ModelMap model) throws Exception { return "stillResult"; } }
ISureService.java
package com.service; public interface ISureService { public int getCount(); }
SureServiceImpl.java
package com.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.mapper.SureMapper; @Service("sureService") public class SureServiceImpl implements ISureService { @Resource private SureMapper sureMapper; public int getCount() { return sureMapper.getTheCount(); } }
SureMapper.java
package com.mapper; public interface SureMapper { public int getTheCount(); }
SureMapper.xml
表名选择任意数据库已经有的表即可。
<?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.mapper.SureMapper" > <select id="getTheCount" resultType="java.lang.Integer"> select count(1) from BOOK </select> </mapper>