开发NetBeansSpringMVC基础入门

Spring框架是一个非常优秀的IoC框架,它可以无缝集成流行的各种前端MVC Web框架和后段的O/R Mapping框架。并且,Spring本身就提供了一个非常优秀的MVC框架。本文将讲述如何在NetBeans中使用Spring MVC框架开发***个具有MVC结构的Web应用程序。

创新互联公司专注于企业成都营销网站建设、网站重做改版、烟台网站定制设计、自适应品牌网站建设、HTML5建站商城开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为烟台等各大城市提供网站开发制作服务。

Spring MVC框架提供了一个基本的DispatcherServlet,所有的请求都通过DispatcherServlet发送到恰当的Controller处理,最终结果由对应的JSP渲染出来。

[***个Spring MVC程序]

我们仍以HelloWorld工程为例。启动NetBeans,打开上次我们创建的HelloWorld工程,右键点击Library,选择Add JAR/Folder:

选择spring.jar,将其添加到Libraray中。

然后,再次右键点击Library,选择Add Library,将JSTL 1.1添加到Libraray中:

下一步,我们新建一个SpringappController.java

 
 
 
  1. package hello;   
  2.  
  3. import org.springframework.web.servlet.mvc.Controller;  
  4.  
  5. import org.springframework.web.servlet.ModelAndView;  
  6.  
  7. import javax.servlet.http.*;   
  8.  
  9. public class SpringappController implements Controller {  
  10.  
  11. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)  
  12.  
  13. throws javax.servlet.ServletException, java.io.IOException  
  14.  
  15. {  
  16.  
  17. return new ModelAndView("view.jsp", "model", "Hello, world!");  
  18.  
  19. }  
  20.  
  21. }  

在这里Controller创建的Model仅仅是一个简单的字符串,实际的应用程序中,Controller应当接收URL参数,然后访问业务层,获得Model。

接着创建view.jsp来渲染结果,用到了jstl标签:

 
 
 
  1. <%@ page session="false"%> 
  2.  
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 
  4.  
  5. <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>   
  6.  
  7. </strong>View.jsp<strong> title> head></strong> </li> <li> </li> <li><strong><body</strong> bgcolor="#FFFFFF"<strong>></strong> </li> <li> </li> <li>Result: <strong><c:out</strong> value="${model}" <strong>/></strong> </li> <li> </li> <li><strong> body> html></strong> </li> </ol></pre><p>下一步是修改web.xml,以“.c”结束的URL会被映射到Spring的DispatcherServlet:</p><pre> <ol> <li><strong><servlet></strong>   </li> <li><strong><servlet-name></strong>springapp<strong> servlet-name></strong>   </li> <li><strong><servlet-class></strong>org.springframework.web.servlet.DispatcherServlet<strong> servlet-class></strong>   </li> <li><strong><load-on-startup></strong>1<strong> load-on-startup></strong>   </li> <li><strong> servlet></strong>   </li> <li> </li> <li><strong><servlet-mapping></strong>   </li> <li><strong><servlet-name></strong>springapp<strong> servlet-name></strong>   </li> <li><strong><url-pattern></strong>*.c<strong> url-pattern></strong>   </li> <li><strong> servlet-mapping></strong>   </li> </ol></pre><p>然后,再添加一个springapp-servlet.xml配置文件,放到web/WEB-INF下: </p><p>内容如下:</p><pre> <ol> <li><strong> xml</strong> version="1.0" encoding="UTF-8"<strong>?></strong> </li> <li> </li> <li><strong>></strong>   </li> <li> </li> <li><strong><beans></strong> </li> <li> </li> <li><strong><bean</strong> id="springappController" class="hello.SpringappController"<strong>/></strong> </li> <li> </li> <li><strong><bean</strong> id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"<strong>></strong> </li> <li> </li> <li><strong><property</strong> name="mappings"<strong>></strong> </li> <li> </li> <li><strong><props></strong> </li> <li> </li> <li><strong><prop</strong> key="/hello.c"<strong>></strong>springappController<strong> prop></strong> </li> <li> </li> <li><strong> props></strong> </li> <li> </li> <li><strong> property></strong> </li> <li> </li> <li><strong> bean></strong> </li> <li> </li> <li><strong> beans></strong>   </li> </ol></pre><p>编译、部署,可以在浏览器中看到结果:Result: Hello, world.</p> <p> 网站题目:<a href="http://www.hantingmc.com/qtweb/news6/84006.html">开发NetBeansSpringMVC基础入门</a> <br> URL链接:<a href="http://www.hantingmc.com/qtweb/news6/84006.html">http://www.hantingmc.com/qtweb/news6/84006.html</a> </p> <p> 网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等 </p> <p class="adpic"> <a href="https://www.cdcxhl.com/service/ad.html" target="_blank" class="ad">广告</a> <a href="" target="_blank" class="adimg"><img src=""></a> </p> <p class="copy"> 声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a> </p> </div> <div class="newsmorelb"> <p>猜你还喜欢下面的内容</p> <ul> <li> <a href="/qtweb/news5/84005.html">Linux教程:轻松安装源码并快速掌握使用方法(linux安装源码)</a> </li><li> <a href="/qtweb/news4/84004.html">我国边缘计算行业趋势:物联网、数据流量驱动市场规模高速扩张</a> </li><li> <a href="/qtweb/news3/84003.html">如何做好应急响应,避免网络安全事件的扩大化工作</a> </li><li> <a href="/qtweb/news2/84002.html">从零开始学习Linux——2019年9月9-9.12的自我挑战</a> </li><li> <a href="/qtweb/news1/84001.html">北京云计算将呈现五大新趋势?(北京云计算将呈现五大新趋势的企业)</a> </li><li> <a href="/qtweb/news0/84000.html">跨境电商为何选择香港作为服务器的首选地方</a> </li><li> <a href="/qtweb/news49/83999.html">如何更好的推广一个产品呢?</a> </li><li> <a href="/qtweb/news48/83998.html">如何在Eclipse中添加数据库:简单易懂的教程(eclipse添加数据库)</a> </li><li> <a href="/qtweb/news47/83997.html">创新互联小程序教程:微信小程序运力方使用·onOrderQuery</a> </li> </ul> </div> </div> <div class="col-lg-3 noneb"> <div class="bkright" style="margin-top: 0"> <p><a href="https://www.cdcxhl.com/news/xiangyingshi/">响应式网站知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news6/129806.html">虚拟机内存占用率高怎么解决?(虚拟主机占用过多服务器资源怎么解决)</a> </li><li> <a class="text_overflow" href="/qtweb/news18/428568.html">【域名查询】如何核查待删除域名?</a> </li><li> <a class="text_overflow" href="/qtweb/news34/485834.html">构建Redis集群一台机器的建立(redis集群单数台)</a> </li><li> <a class="text_overflow" href="/qtweb/news33/177083.html">Redis限流官方文档指南(redis限流官方文档)</a> </li><li> <a class="text_overflow" href="/qtweb/news0/164800.html">可以禁用Gzip的一种情况</a> </li><li> <a class="text_overflow" href="/qtweb/news30/223030.html">如何在ASP.NETCore6中使用跟踪侦听器</a> </li><li> <a class="text_overflow" href="/qtweb/news21/19871.html">Windows宝塔IIS配置文件格式说明</a> </li><li> <a class="text_overflow" href="/qtweb/news15/496365.html">香港高防服务器租用时要注意什么</a> </li><li> <a class="text_overflow" href="/qtweb/news29/531979.html">阿里云CN2怎么购买?全面解析阿里云CN2的购买流程及注意事项</a> </li><li> <a class="text_overflow" href="/qtweb/news23/431273.html">研究Redis用源码攻克电脑之谜(redis电脑源码)</a> </li><li> <a class="text_overflow" href="/qtweb/news45/430395.html">CPanel安装教程</a> </li><li> <a class="text_overflow" href="/qtweb/news32/171282.html">2010年将是Java模块化的一年</a> </li><li> <a class="text_overflow" href="/qtweb/news32/61082.html">怎么把电脑密码取消掉</a> </li><li> <a class="text_overflow" href="/qtweb/news39/484789.html">企业邮箱设置?(企业邮箱下坠怎么设置密码)</a> </li><li> <a class="text_overflow" href="/qtweb/news28/121028.html">LinuxC程序设计中的互斥锁(linuxc互斥锁)</a> </li> </ul> </div> <div class="bkright tag"> <p><a href="https://www.cdcxhl.com/hangye/link.html" target="_blank">各行业网站</a></p> <ul> <li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/rxfhw/" target="_blank">柔性防护网</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/snjbc/" target="_blank">水泥搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sqwhq/" target="_blank">社区文化墙</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zbljbc/" target="_blank">自拌料搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/banjia/" target="_blank">搬家公司</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/loutihulan/" target="_blank">楼梯护栏</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zhanting/" target="_blank">展览展示</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/yangguangfang/" target="_blank">阳光房</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/pvc/" target="_blank">PVC花箱</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/fwjd/" target="_blank">房屋鉴定</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/xiangsu/" target="_blank">橡塑保温</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/qzgqb/" target="_blank">轻质隔墙板</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/opp/" target="_blank">OPP胶袋</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/hntjbg/" target="_blank">混凝土搅拌罐</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ggsj/" target="_blank">广告设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zufadianji/" target="_blank">发电机租赁</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="https://www.scvps.cn/" target="_blank">服务器租凭</a>    <a href="https://www.cdxwcx.com/jifang/deyang.html" target="_blank">德阳服务器托管</a>    <a href="https://www.cdxwcx.com/tuiguang/" target="_blank">成都网络推广</a>    <a href="https://www.cdxwcx.com/wangzhan/shop.html" target="_blank">电商网店</a>    <a href="http://www.scghjhjc.cn/" target="_blank">四川广汉锦华</a>    <a href="http://www.024fuxl.com/" target="_blank">成都学生服定制</a>    <a href="http://chengdu.cdcxhl.com/dingzhi/" target="_blank">定制网站</a>    <a href="http://www.cxjianzhan.com/" target="_blank">网络营销推广</a>    <a href="http://www.36103.cn/baojia/" target="_blank">网站设计制作报价</a>    <a href="http://www.sccummins.cn/" target="_blank">成都康明斯发电机</a>    <a href="https://www.cdxwcx.com/jifang/mianyang.html" target="_blank">绵阳托管服务器</a>    <a href="http://www.scbaiwuyu.cn/" target="_blank">四川白乌鱼</a>    <a href="http://www.cdxwcx.cn/tuoguan/neijiang.html" target="_blank">内江托管服务器</a>    <a href="https://www.cdxwcx.com/wangzhan/mbyidong.html" target="_blank">wap手机网站</a>    <a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌优化排名</a>    <a href="http://www.cqcxhl.com/" target="_blank">网站建设</a>    <a href="http://www.lzwzjz.cn/" target="_blank">泸州网站建设</a>    <a href="http://www.kswcd.com/mobile/" target="_blank">手机网站建设</a>    <a href="http://www.4006tel.net/" target="_blank">网站开发</a>    <a href="http://www.cxhljz.cn/app/" target="_blank">Android开发</a>     </div> </div> <footer> <div class="carousel-inner footjz"> <div class="container"> <i class="icon iconfont zbw"></i> 高品质定制 <i class="icon iconfont"></i> 跨终端自动兼容 <i class="icon iconfont"></i> 节约开发成本 <i class="icon iconfont"></i> 开发周期短 <i class="icon iconfont"></i> 一体化服务 <button type="button" class="btn btn-default btn-lg" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 立即开始2800定制网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 2800定制网站建设</button> </div> </div> <div class="carousel-inner bqsy"> <div class="container"> <div class="lxfs"> <h4 class="yutelnone">028-86922220 13518219792</h4> <h4 class="yutelblock"><a href="tel:02886922220">028-86922220</a> <a href="tel:13518219792">13518219792</a></h4> <a class="btn btn-default" href="tencent://message/?uin=532337155&Site=&Menu=yes" role="button">网站建设<span>QQ</span>:532337155</a> <a class="btn btn-default" href="tencent://message/?uin=631063699&Site=&Menu=yes" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=532337155&version=1&src_type=web&web_src=oicqzone.com" role="button">网站制作<span>QQ</span>:532337155</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=631063699&version=1&src_type=web&web_src=oicqzone.com" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn-default nonea" href="tencent://message/?uin=1683211881&Site=&Menu=yes" role="button">售后QQ:1683211881</a> <div class="dz">创新互联建站专注: <a href="https://www.cdcxhl.com/" target="_blank">网站设计</a> <a href="https://www.cdcxhl.com/" target="_blank">网站制作</a> <a href="https://www.cdcxhl.com/" target="_blank">网站建设</a> <address>地址:成都太升南路288号锦天国际A幢10楼</address> </div> </div> <div class="bzdh dz"><img src="https://www.cdcxhl.com/imges/bottom_logo.png" alt="创新互联"> <p><a href="https://www.cdcxhl.com/menu.html" target="_blank">成都创新互联科技有限公司</a><br> Tel:400-028-6601(7x24h)</p></div> </div> </div> </footer> </body> </html> <script> $.getJSON ("../../qtwebpic.txt", function (data) { var jsonContent = { "featured":data } var random = jsonContent.featured[Math.floor(Math.random() * jsonContent.featured.length)]; $(".adpic .adimg").attr("href",random.link) $(".adpic img").attr("src",random.pic); }) </script>