SSM(Spring + SpringMVC + MyBatis)框架使用

SSM(Spring + SpringMVC + MyBatis)框架前后端交互使用

程序框架
这里写图片描述

1、前端页面入口
在jsp文件中,通过下面代码进入页面

<li><a href="${proPath}/cost/index_cost" target="_blank"><img class="list_img" src="${resPath}/img/calculator.png"/></a></li>

其中,{proPath}表示系统路径,
proPath=”{pageContext.request.contextPath}”;
resPath=”{pageContext.request.contextPath}/res”;

系统在后台查找Control 模块,根据cost/index_cost 进入costController.java进行后台数据处理。
后台控制层代码:

@Controller
@RequestMapping("/cost")
public class CostController {
    private static Logger log = Logger.getLogger(CostController.class.getName());

    @Resource
    private RentAndLabourCostService rentAndLabourCostsService;

    @RequestMapping("/index_cost")
    public String IncomeLogin(HttpServletRequest request, Model model) {
        //tab 第一页
        Integer pondid = 1; 
        List<RentAndLabourCost> allCost = this.rentAndLabourCostsService.selectAllHistoryData();
        model.addAttribute("historyAll",allCost);   
        System.out.println( "cost income test!" + allCost );
        return "index_income";
    }
}   

(1)根据注解@RequestMapping(“/cost”)查找得到对应的类,然后根据@RequestMapping(“/index_cost”)查找得到对应的方法处理;
注意:此时函数处没有 @ResponseBody注解。
(2)进入函数
public String IncomeLogin(HttpServletRequest request, Model model)
在该函数中,request表示从前端通过request获取的参数,此处未使用;model表示将后台参数传递到前端,通过 model.addAttribute(“totalRentValue”, totalRentValue);实现。
(3)返回值为字符串类型
return “index_income”;
此时系统根据返回的字符串通过web.xml静态资源过滤查找到相应的前端文件。即为index_income.jsp。
web.xml静态资源过滤为:

 <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</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>*.jpg</url-pattern>
  </servlet-mapping>

(5)后台获取到数据后通过model传递到前端。
以租金成本为例:

float totalRentValue = rentAndLabourCostsService.selectSumByType(1,pondid);
        model.addAttribute("totalRentValue", totalRentValue);

其中addAttribute的第一个参数为key,第二个为value,前端处理通过EL表达式可以直接通过key获取value的值。

<span>租金</span><span style="margin-left: 20%"                                                           id="rentValue">${totalRentValue}</span><span></span>

历史记录获得的结果为List形式存储,前端采用EL表达式循环显示。
后台:

List<RentAndLabourCost> allCost = this.rentAndLabourCostsService.selectAllHistoryData();
        model.addAttribute("historyAll",allCost);

前端:在页面初始状态下,显示数据库中所有的历史记录信息。

<div id="history_list" data-spy="scroll" data-target="#navbar-example" data-offset="0" 
     style="height:400px;overflow:auto; position: relative;">
<c:forEach items="${historyAll}" var="allCost">    
<div class="form-group"  >
<!-- 记录 -->
<label class="col-md-3 control-label margin-3" id ="historyTime"><fmt:formatDate value="${allCost.sysTime}" type="both" /> </label>
<span class="col-md-3 control-label" id="historyPond"><c:if test="${allCost.pondid == 1}">a</c:if><c:if test="${allCost.pondid == 2}">b</c:if><c:if test="${allCost.pondid == 3}">c</c:if></span> 
<span   class="col-md-3 control-label"id ="historyName"><c:if test="${allCost.fixcosttype == 1}">11</c:if><c:if test="${allCost.fixcosttype == 2}">22</c:if><c:if test="${allCost.fixcosttype == 3}">33</c:if><c:if test="${allCost.fixcosttype == 4}">44</c:if><c:if test="${allCost.fixcosttype == 5}">55</c:if></span>
<span   class="col-md-1 control-label span_danger" id="historyType"><c:if test="${allCost.status == 1}">+</c:if><c:if test="${allCost.status == 0}">-</c:if></span> 
<span   class="col-md-2 control-label span_danger margin-3" id="historyValue">${allCost.value}</span>
<span class="col-md-1 control-label margin-5"></span>
</div>
</c:forEach>
</div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本项目是用的spring springMVC myBatis框架,前段没用框架,只写了简单的页面效果,做增删查改 这是我系统学习所得,希望能对你有所帮助 项目部署: 1;导包,就是web-inf下lib,让后把这些选,单击右键build path -->add library 2;web.xml配置文件,这里几乎不需要改,以后要用只需要复制就行 3;具体框架配置文件,都集在了sourceConfig文件夹下,这个自己琢磨,以后再用这个框架也是几乎不需要改的,之所以说几乎,是因为只需要改包名就可以了 4;写bean,写dao,写service,写controller,这是重点 5;bean下写要操作的数据表,记住这个类药与数据库的表一致,最好一模一样,避免麻烦 6;dao下写数据库操作内容,那下面有两个文件名一样的,一个是java文件,一个是xml文件,java文件是定义方法名,xml文件是让你写具体的数据操作方法的,格式就是这样,你看看就能懂,你只需要这样写,这个框架就可以识别,吧你在xml写的数据库操作方法匹配到java文件的方法,这是mybatis做的事 7;service包放你的业务逻辑层,具体来说就是,dao只放数据操作,service引用数据操作,还可以有其他操作 8;controller类的格式你要用心点了,这个是控制器,控制请求的跳转,他和servlet的功能类似, 功能引导: 为了让你更方便了解这个项目,说一下流程 1.把项目部署到tomcat,启动服务, 2.在浏览器输入http://localhost:8080/AscentSys/user/in.do 3.这个命令的意思是,访问这个项目地址user/in.do,然后这个请求就会发送到controller,在controller首先匹配到user,在匹配到in.do就调到具体的方法去处理这个情求了,这里会跳转到users文件夹下的login.jsp页面,解释一下,在spring-servlet.xml配置文件有一句路径解析的,“前缀后缀”那个bean,意思是在返回的东西加前缀后缀,这里写的是啥你琢磨琢磨可以明白的 4.在login.jsp页面,有个提交地址,是login.do,按上面说的,在controller首先匹配到user,在匹配到login.do就调到具体的方法去处理这个情求了,后面的流程我就不说了,自己看 5。再说一点,return的好多字符串,有的是redirect:/user/userlist.do这样的格式,意思是转发,就是转到另一个请求去,同理,具体意思是,在controller首先匹配到user,在匹配到userlist.do就调到下面的方法去处理这个情求了, 6,关于传参数的问题,在表单写的属性,在controller自动接收,也可以接受user对象,如果是对象,那个表单的格式你要看仔细了和一般表单的不同之处。琢磨琢磨你会明白的, 希望能对你有所帮助
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值