欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring MVC 与 Struts2 功能实现对比

程序员文章站 2022-07-08 16:43:39
...

上一篇文章 [ 框架第十五天 ],直接贴出代码SSH(Spring MVC)框架如何搭建,其实关键是配件文件如何配置,接下来主要介绍Spring MVC 与 Struts2 功能实现的写法对比

因在Spring MVC中不能用Struts2的标签,Spring MVC上传和下载文件,还有部门列表显示等等功能和Struts2有很大不用,而且颇有意思,因此下面用同一个项目对比他们的不同

框架:

  • Spring MVC + Spring + Hibernate
  • Struts2 + Spring + Hibernate
功能对比

1、添加与修改页面

  • 性别显示

Struts2:(用Struts2标签多好...)

//添加页面
<s:radio 
list="#{'男':'男','女':'女'}" 
name="psex" 
label="性别" 
value="'女'">
</s:radio>
//--------------------------------------------------------------------------
//修改页面(回显!)
<s:radio 
list="#{'男':'男','女':'女'}" 
name="per.psex" 
label="性别" 
value="'女'">
</s:radio>

Spring MVC:(平平淡淡才是真!)

<tr>
    <td align="right">性别:</td>
    <td><input type="radio" name="psex" value="男">男
        <input type="radio" name="psex" value="女">女</td>
</tr>
//--------------------------------------------------------------------------
//修改页面(回显!)
<tr>
    <td align="right">性别:</td>
    <td><input type="radio" name="psex" value="男" ${per.psex=='男'?"checked='checked'":""}>男
        <input type="radio" name="psex" value="女" ${per.psex=='女'?"checked='checked'":""}>女
    </td>
</tr>
  • 个人特长

Struts2:

<s:checkboxlist 
list="#{'足球':'足球','篮球':'篮球','唱歌':'唱歌','武术':'武术'}" 
name="skilled" 
label="个人特长" 
value="{'篮球','武术'}">
</s:checkboxlist>
//--------------------------------------------------------------------------
//修改页面(回显!)
<s:checkboxlist 
list="#{'足球':'足球','篮球':'篮球','唱歌':'唱歌','武术':'武术'}" 
name="per.skilled" 
label="个人特长" 
value="{'篮球','武术'}">
</s:checkboxlist>

Spring MVC:

<tr>
    <td align="right">个人特长:</td>
    <td>
        <input type="checkbox" name="skilled" value="足球">足球
        <input type="checkbox" name="skilled" value="篮球">篮球 
        <input type="checkbox" name="skilled" value="唱歌" checked="checked">唱歌
        <input type="checkbox" name="skilled" value="武术" checked="checked">武术
    </td>
</tr>
//--------------------------------------------------------------------------
//修改页面(回显!)
<tr>
    <td align="right">个人特长:</td>
    <td>
        <input type="checkbox" name="skilled" value="足球" ${b=='足球'?"checked='checked'":"" }>足球
        <input type="checkbox" name="skilled" value="篮球" ${d=='篮球'?"checked='checked'":""}>篮球 
        <input type="checkbox" name="skilled" value="唱歌" checked="checked" ${c=='唱歌'?"checked='checked'":"">唱歌
        <input type="checkbox" name="skilled" value="武术" checked="checked" ${a== '武术'?"checked='checked'":""}>武术
    </td>
</tr>
//---------------------------------------------------------------------------
//PersonAction中的toUpdate方法
        String[] aa = per.getSkilled().split(",");
        for (int i = 0; i < aa.length; i++) {
            if (aa[i].equals("武术")) {
                request.setAttribute("a", aa[i]);
            } else if (aa[i].equals("足球")) {
                request.setAttribute("b", aa[i]);
            } else if (aa[i].equals("唱歌")) {
                request.setAttribute("c", aa[i]);
            } else if (aa[i].equals("篮球")) {
                request.setAttribute("d", aa[i]);
            }
        }
  • 学历

Struts2:

<s:select
list="#{'':'请选择','本科':'本科','专科':'专科','研究生':'研究生','博士':'博士' }"
name="degree" 
label="学历" 
value="'本科'">
</s:select>
//--------------------------------------------------------------------------
//修改页面(回显!)
<s:select
list="#{'':'请选择','本科':'本科','专科':'专科','研究生':'研究生','博士':'博士' }"
name="per.degree" 
label="学历" 
value="'本科'">
</s:select>

Spring MVC:

<tr>
     <td align="right">学历:</td>
     <td>
       <select name="degree">
          <option>博士</option>
          <option>研究生</option>
          <option>本科</option>
          <option>专科</option>
       </select>
     </td>
</tr>
//--------------------------------------------------------------------------
//修改页面(回显!)
<tr>
     <td align="right">学历:</td>
     <td>
       <select name="degree">
          <option ${per.degree== "博士"?"selected='selected'":""}>博士</option>
          <option ${per.degree== "研究生"?"selected='selected'":"">研究生</option>
          <option ${per.degree== "本科"?"selected='selected'":"">本科</option>
          <option ${per.degree== "专科"?"selected='selected'":"">专科</option>
       </select>
     </td>
</tr>
  • 简历

Struts2:

<s:textarea
name="resume" 
cols="20" 
rows="6" 
label="简历">
</s:textarea>
//--------------------------------------------------------------------------
//修改页面(回显!)
<s:textarea
name="per.resume" 
cols="20" 
rows="6" 
label="简历">
</s:textarea>

Spring MVC:

<tr>
    <td align="right">简历:</td>
    <td>
        <textarea name="resume" cols="20" rows="6"></textarea>
    </td>
</tr>
//--------------------------------------------------------------------------
//修改页面(回显!)
<tr>
    <td align="right">简历:</td>
    <td>
        <textarea name="resume" cols="20" rows="6">${per.resume }</textarea>
    </td>
</tr>
  • 部门

Struts2:

<s:select 
list="list" 
label="部门" 
name="dept.did" 
listKey="did" 
listValue="dname" />
//--------------------------------------------------------------------------
//修改页面(回显!)
<s:select 
list="list" 
label="部门" 
name="per.dept.did" 
listKey="did" 
listValue="dname" />

Spring MVC:

//---------------------------------JS部分-----------------------------------
<script type="text/javascript">
    $(function() {
        $.post("per/dept.do", function(msg) {
            for (i = 0; i < msg.length; i++) {
                $("#querypdept").append(
                        "<option value="+msg[i].did+">" + msg[i].dname
                                + "</option>");
            }
        });
    });
</script>
//---------------------------------Body部分---------------------------------
<tr>
    <td align="right">部门:</td>
    <td><select name="dept.did" id="querypdept"></select>
    </td>
</tr>
//-------------------------------PersonAction部分---------------------------
    @RequestMapping("toadd")
    public String toAdd() throws Exception {// 到添加
        List<Dept> list = new ArrayList<Dept>();
        list = ds.list();// 查询出所有部门表
        return "add";
    }
//--------------------------------------------------------------------------
//修改页面(回显!)
//---------------------------------JS部分-----------------------------------
<script type="text/javascript">
    $(function() {
        $.post("per/dept.do", function(msg) {
            var aa = "${per.dept.did}";
            for (i = 0; i < msg.length; i++) {
                $("#querypdept").append(
                        "<option value="+msg[i].did+">" + msg[i].dname
                                + "</option>");
                if (msg[i].did == aa) {
                    $("#querypdept option[value='"+msg[i].did+"']").attr('selected','selected');
                }
            }

        });
    });
</script>
//---------------------------------Body部分---------------------------------
<tr>
    <td align="right">部门:</td>
    <td><select name="dept.did" id="querypdept"></select>
    </td>
</tr>
  • 上传下载图片和Excel文件(颇有意思)

Struts2:

//上传图片
<s:file name="myfile" label="上传照片"></s:file>
//--------------------------------------------------------------------------
//修改页面(回显!)
 <s:file name="myfile" label="上传照片"></s:file>
<img 
src="<%=request.getContextPath()%>/upload/${per.filepath}" 
width="70" 
height="80">
//上传Excel文件
//---------------------------------JS部分-----------------------------------
    </script>
    <script type="text/javascript">
        $(function() {
            $("#btn7").click(function() {
                location = "person_exportExcel";
            });
            $("#btn6").click(function() {
                var aa = $("#upload").val();
                if (aa != "") {
                    kkk.submit();
                } else {
                    alert("请选择Excel文件!");
                }
            });
        });
    </script>
//---------------------------------Body部分---------------------------------
<td colspan="3">
     <s:form action="person_importExcel"
        method="post" name="kkk" enctype="multipart/form-data">
        <input type=file name="myfile" id="upload">
        <input type="button" value="导入Excel" id="btn6">;
        <input type="button" value="导出Excel表" id="btn7">
     </s:form>
</td>

Spring MVC:

<tr>
    <td align="right">上传照片:</td>
    <td><input type="file" name="file"></td>
</tr>
//--------------------------------------------------------------------------
//修改页面(回显!)
<input type="file" name="file">
<img
src="<%=request.getContextPath()%>/upload/${per.filepath}"
width="85" 
height="100">
//上传Excel文件
//---------------------------------JS部分-----------------------------------
    <script type="text/javascript">
        $(function() {
            $("#btn7").click(function() {
                location = "per/exportExcel.do";
            });
            $("#btn6").click(function() {
                var aa = $("#upload").val();
                if (aa != "") {
                    kkk.submit();
                } else {
                    alert("请选择Excel文件!");
                }
            });
        });
    </script>
//---------------------------------Body部分---------------------------------
<td colspan="3"><form action="per/importExcel.do" method="post" name="kkk" enctype="multipart/form-data">
    <input type=file name="file" id="upload"> 
    <input type="button" value="导入Excel" id="btn6">;
    <input type="button" value="导出Excel表" id="btn7">
</form></td>

颇有意思的部分来了:在Struts2框架中,有FileUpload2工具类,但是在Spring MVC中却稍显复杂

//Struts2上传:
        filename=FileUpload2.upload(filename, myfile);//完成上传
        per.setFilepath(filename);//设置路径
//Spring MVC 上传:
    @RequestMapping("add")
    public String add(
            @RequestParam(value = "file", required = false) MultipartFile file,
            HttpServletRequest request, Person user) throws Exception {// 添加
        
        String path = request.getSession().getServletContext()
                .getRealPath("upload");
        
        String fileName = file.getOriginalFilename();
        
        // 解决文件同名问题
        fileName = UUID.randomUUID().toString().replace("-", "")
                + fileName.substring(fileName.lastIndexOf("."));
        File targetFile = new File(path, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        // 保存
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("fileUrl", request.getContextPath() + "/upload/"
                + fileName);
        user.setFilepath(fileName);
        /*
         * 如果不涉及上传则表单中不能设置enctype="multipart/form-data", 本方法中只需保留Person
         * pe和以下2行代码即可
         */
        ps.add(user);
        return "redirect:listPerson.do";
    }

至此,Spring MVC与Struts2 对比完毕,文章略长,但值得记录
献上两个项目的源码

链接: http://pan.baidu.com/s/1cBt0Z0 密码: ahir