有句话这么说的:程序员的能力不在于增加代码的能力,而在于减少代码的能力。基于这个我认之为真的命题,我经常问和我一起工作的程序员:你的程序还能不能精简一点?如果能,那能不能再精简一点?

要减少程序,往往建立在程序员对自己的程序代码本身、对业务,以及对所使用的技术框架都非常的熟悉的情况下,而这些就是一个程序员的综合能力。

前段时间在做Spring培训的时候,我给学员们出了一道题,用于检查学员对Spring了解的深度。各位读者不妨也来试试,题目是这样的:

对于以下Github程序:

https://github.com/davenkin/springmvc4-helloworld

对于所有文件,只删除文件或者文件里的某些行,不能修改某行,不能增加行,不能增加文件,在程序运行时,依然能看到页面上的"Hello World!", 能删除文件行或文件最多者胜出.

要运行该程序,git clone代码后,将命令行切换到springmvc4-helloworld目录,对于Linux用户,运行:

./gradlew jettyRun

对于Windows用户,运行:

gradlew.bat jettyRun

初次执行可能会比较慢, 因为gradle会从网上下载很多依赖。之后打开http://localhost:8080/springmvc4-helloworld/hello,你将看到页面上的“Hello World !”。

这个程序本身其实是一个很简单的HelloWorld程序,但是却包含了Spring的很多内在工作机制以及框架约定(Convention over Configuration),这些机制和约定没有搞清楚,我就不能说你对Spring了解得有多深。

原程序主要的目录结构如下:

src
└── main
├── java
│   └── hello
│   └── HomeController.java
├── resources
│   └── applicationContext.xml
└── webapp
└── WEB-INF
├── jsp
│   └── hello.jsp
├── spring-servlet.xml
└── web.xml

一个标准的Java Web项目,web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>hello</display-name> <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> <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

对应的spring-servlet.xml文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="hello"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>

另外,还有另一个Spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> </beans>

我们定义了一个HomeController:

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
@RequestMapping("/")
public class HomeController { @RequestMapping(value = "hello", method = RequestMethod.GET)
public String hello(Model model) {
model.addAttribute("info", "Hello World!");
return "/WEB-INF/jsp/hello.jsp";
}
}

该HomeController所对应的View为hello.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:out value="${info}"/><p>
</body>
</html>

最新文章

  1. [斜率优化DP]【学习笔记】【更新中】
  2. Spring.Scheduling.Quartz 作业的应用(定时任务和循环触发任务)
  3. 在JavaScript中,arguments是对象的一个特殊属性。
  4. 编译预处理命令--define和ifdef的使用
  5. js 传参数
  6. Problem:To Connect with MySQL in Virtual PC Environment
  7. 推送:腾迅信鸽 VS Bmob
  8. [置顶] 如何高效使用和管理Bitmap--图片缓存管理模块的设计与实现
  9. 【Java基础】String StringBuffer StringBuilder
  10. PHP面试随笔
  11. 自动化服务部署(二):Linux下安装jenkins
  12. D. Frets On Fire 前缀和+二分
  13. 解决No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.跨域问题(后台(java)解决方法)
  14. TNS-12535/12606 and ORA-3136 on Connection to Database (Doc ID 2313573.1)
  15. myBatis之Clob &amp; Blob
  16. [JavaScript] 邮箱验证
  17. [BAT] xcopy拷贝远程服务器共享文件到本地
  18. 安装itunes时提示不能删除旧版本的apple应用程序支持
  19. [转]eclipse导入V7包出现错误解决办法
  20. QTableWidget与QTableView的区别

热门文章

  1. Jquery mobiscroll 移动设备(手机)wap日期时间选择插件以及滑动、滚动插件
  2. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
  3. Android混合开发之WebView与Javascript交互
  4. ASP.NET Core应用中如何记录和查看日志
  5. 谱聚类(spectral clustering)原理总结
  6. 一个技术汪的开源梦 —— 公共组件缓存之分布式缓存 Redis 实现篇
  7. Autofac - 生命周期
  8. MySQL ALTER命令
  9. 如何查看w3p.exe 和IIS 应用程序池的关系
  10. .NET Portability Analyzer 已开源