km之路--009 spring mvc 起手

一个最简单的HelloWorld

目录结构

pom

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.laolang.km</groupId>
  5     <artifactId>km-blog</artifactId>
  6     <packaging>war</packaging>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <!-- 集中定义依赖版本号 -->
  9     <properties>
 10         <!-- test -->
 11         <junit.version>4.10</junit.version>
 12         <hamcrest.version>1.3</hamcrest.version>
 13
 14
 15         <!-- java ee -->
 16         <javaee-api.version>7.0</javaee-api.version>
 17         <servlet-api.version>3.0.1</servlet-api.version>
 18         <persistence-api.version>1.0</persistence-api.version>
 19         <jstl.version>1.2</jstl.version>
 20         <standard.version>1.1.2</standard.version>
 21
 22
 23         <!-- spring -->
 24         <spring.version>4.2.0.RELEASE</spring.version>
 25
 26         <!-- mybatis -->
 27         <mybatis.version>3.2.8</mybatis.version>
 28         <mybatis.spring.version>1.2.2</mybatis.spring.version>
 29         <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
 30         <pagehelper.version>4.0.3</pagehelper.version>
 31         <tk.mapper.version>3.2.2</tk.mapper.version>
 32
 33
 34         <mysql.version>5.1.32</mysql.version>
 35
 36         <!-- log -->
 37         <slf4j.version>1.7.12</slf4j.version>
 38         <logback.version>1.1.3</logback.version>
 39         <logback.ext.version>0.1.2</logback.ext.version>
 40
 41         <!-- json -->
 42         <jackson.version>2.4.2</jackson.version>
 43         <jackson.mapper.version>1.9.13</jackson.mapper.version>
 44
 45         <!-- google -->
 46         <gson.version>2.2.2</gson.version>
 47         <guava.version>18.0</guava.version>
 48
 49         <!-- alibaba -->
 50         <druid.version>1.0.12</druid.version>
 51
 52         <!-- ehcache -->
 53         <ehcache.version>2.7.5</ehcache.version>
 54
 55         <!-- apache -->
 56         <httpclient.version>4.5</httpclient.version>
 57         <commons-lang3.version>3.3.2</commons-lang3.version>
 58         <commons-io.version>2.5</commons-io.version>
 59         <commons-fileupload.version>1.3.2</commons-fileupload.version>
 60         <commons-net.version>3.3</commons-net.version>
 61         <commons-codec.version>1.9</commons-codec.version>
 62         <shiro.version>1.3.2</shiro.version>
 63         <activemq.version>5.12.0</activemq.version>
 64
 65         <!-- beetl -->
 66         <beetl.version>2.7.5</beetl.version>
 67
 68         <!-- JSR 303 -->
 69         <hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
 70         <validator-api.version>1.1.0.Final</validator-api.version>
 71
 72         <joda-time.version>2.5</joda-time.version>
 73     </properties>
 74     <dependencies>
 75         <!-- test -->
 76             <dependency>
 77                 <groupId>junit</groupId>
 78                 <artifactId>junit</artifactId>
 79                 <version>${junit.version}</version>
 80                 <scope>test</scope>
 81             </dependency>
 82             <dependency>
 83                 <groupId>org.hamcrest</groupId>
 84                 <artifactId>hamcrest-all</artifactId>
 85                 <version>${hamcrest.version}</version>
 86                 <scope>test</scope>
 87             </dependency>
 88
 89             <!-- java ee-->
 90             <dependency>
 91                 <groupId>javax</groupId>
 92                 <artifactId>javaee-api</artifactId>
 93                 <version>${javaee-api.version}</version>
 94                 <scope>provided</scope>
 95             </dependency>
 96             <dependency>
 97                 <groupId>javax.servlet</groupId>
 98                 <artifactId>javax.servlet-api</artifactId>
 99                 <version>${servlet-api.version}</version>
100                 <scope>provided</scope>
101             </dependency>
102             <dependency>
103                 <groupId>javax.persistence</groupId>
104                 <artifactId>persistence-api</artifactId>
105                 <version>1.0</version>
106             </dependency>
107             <dependency>
108                 <groupId>jstl</groupId>
109                 <artifactId>jstl</artifactId>
110                 <version>${jstl.version}</version>
111             </dependency>
112             <dependency>
113                 <groupId>taglibs</groupId>
114                 <artifactId>standard</artifactId>
115                 <version>${standard.version}</version>
116             </dependency>
117
118             <!-- Spring -->
119             <dependency>
120                 <groupId>org.springframework</groupId>
121                 <artifactId>spring-context</artifactId>
122                 <version>${spring.version}</version>
123             </dependency>
124             <dependency>
125                 <groupId>org.springframework</groupId>
126                 <artifactId>spring-oxm</artifactId>
127                 <version>${spring.version}</version>
128             </dependency>
129             <dependency>
130                 <groupId>org.springframework</groupId>
131                 <artifactId>spring-tx</artifactId>
132                 <version>${spring.version}</version>
133             </dependency>
134             <dependency>
135                 <groupId>org.springframework</groupId>
136                 <artifactId>spring-jdbc</artifactId>
137                 <version>${spring.version}</version>
138             </dependency>
139             <dependency>
140                 <groupId>org.springframework</groupId>
141                 <artifactId>spring-beans</artifactId>
142                 <version>${spring.version}</version>
143             </dependency>
144             <dependency>
145                 <groupId>org.springframework</groupId>
146                 <artifactId>spring-aop</artifactId>
147                 <version>${spring.version}</version>
148             </dependency>
149             <dependency>
150                 <groupId>org.springframework</groupId>
151                 <artifactId>spring-test</artifactId>
152                 <version>${spring.version}</version>
153             </dependency>
154             <dependency>
155                 <groupId>org.springframework</groupId>
156                 <artifactId>spring-aspects</artifactId>
157                 <version>${spring.version}</version>
158             </dependency>
159             <dependency>
160                 <groupId>org.springframework</groupId>
161                 <artifactId>spring-web</artifactId>
162                 <version>${spring.version}</version>
163             </dependency>
164             <dependency>
165                 <groupId>org.springframework</groupId>
166                 <artifactId>spring-webmvc</artifactId>
167                 <version>${spring.version}</version>
168             </dependency>
169             <dependency>
170                 <groupId>org.springframework</groupId>
171                 <artifactId>spring-core</artifactId>
172                 <version>${spring.version}</version>
173             </dependency>
174             <!--<dependency>-->
175                 <!--<groupId>org.springframework</groupId>-->
176                 <!--<artifactId>spring-jms</artifactId>-->
177             <!--</dependency>-->
178             <!--<dependency>-->
179                 <!--<groupId>org.springframework</groupId>-->
180                 <!--<artifactId>spring-context-support</artifactId>-->
181             <!--</dependency>-->
182
183
184
185             <!-- JSR 303 -->
186             <dependency>
187                 <groupId>javax.validation</groupId>
188                 <artifactId>validation-api</artifactId>
189                 <version>${validator-api.version}</version>
190             </dependency>
191
192             <dependency>
193                 <groupId>org.hibernate</groupId>
194                 <artifactId>hibernate-validator</artifactId>
195                 <version>${hibernate-validator.version}</version>
196             </dependency>
197
198
199             <!-- beetl -->
200             <dependency>
201                 <groupId>com.ibeetl</groupId>
202                 <artifactId>beetl</artifactId>
203                 <version>${beetl.version}</version>
204             </dependency>
205
206             <!-- Mybatis -->
207             <dependency>
208                 <groupId>org.mybatis</groupId>
209                 <artifactId>mybatis</artifactId>
210                 <version>${mybatis.version}</version>
211             </dependency>
212             <dependency>
213                 <groupId>org.mybatis</groupId>
214                 <artifactId>mybatis-spring</artifactId>
215                 <version>${mybatis.spring.version}</version>
216             </dependency>
217             <dependency>
218                 <groupId>com.github.pagehelper</groupId>
219                 <artifactId>pagehelper</artifactId>
220                 <version>${pagehelper.version}</version>
221             </dependency>
222             <dependency>
223                 <groupId>tk.mybatis</groupId>
224                 <artifactId>mapper</artifactId>
225                 <version>${tk.mapper.version}</version>
226             </dependency>
227
228
229             <!-- MySql -->
230             <dependency>
231                 <groupId>mysql</groupId>
232                 <artifactId>mysql-connector-java</artifactId>
233                 <version>${mysql.version}</version>
234             </dependency>
235
236             <!-- alibaba -->
237             <dependency>
238                 <groupId>com.alibaba</groupId>
239                 <artifactId>druid</artifactId>
240                 <version>${druid.version}</version>
241             </dependency>
242
243             <!-- Jackson Json处理工具包 -->
244             <dependency>
245                 <groupId>org.codehaus.jackson</groupId>
246                 <artifactId>jackson-mapper-asl</artifactId>
247                 <version>${jackson.mapper.version}</version>
248             </dependency>
249             <dependency>
250                 <groupId>com.fasterxml.jackson.core</groupId>
251                 <artifactId>jackson-core</artifactId>
252                 <version>${jackson.version}</version>
253             </dependency>
254             <dependency>
255                 <groupId>com.fasterxml.jackson.core</groupId>
256                 <artifactId>jackson-databind</artifactId>
257                 <version>${jackson.version}</version>
258             </dependency>
259             <dependency>
260                 <groupId>com.fasterxml.jackson.core</groupId>
261                 <artifactId>jackson-annotations</artifactId>
262                 <version>${jackson.version}</version>
263             </dependency>
264
265             <!-- google -->
266             <dependency>
267                 <groupId>com.google.code.gson</groupId>
268                 <artifactId>gson</artifactId>
269                 <version>${gson.version}</version>
270             </dependency>
271             <dependency>
272                 <groupId>com.google.guava</groupId>
273                 <artifactId>guava</artifactId>
274                 <version>${guava.version}</version>
275             </dependency>
276
277             <!-- ehcache -->
278             <dependency>
279                 <groupId>net.sf.ehcache</groupId>
280                 <artifactId>ehcache</artifactId>
281                 <version>${ehcache.version}</version>
282             </dependency>
283
284             <!-- apache -->
285             <dependency>
286                 <groupId>org.apache.commons</groupId>
287                 <artifactId>commons-lang3</artifactId>
288                 <version>${commons-lang3.version}</version>
289             </dependency>
290             <dependency>
291                 <groupId>commons-io</groupId>
292                 <artifactId>commons-io</artifactId>
293                 <version>${commons-io.version}</version>
294             </dependency>
295             <dependency>
296                 <groupId>commons-fileupload</groupId>
297                 <artifactId>commons-fileupload</artifactId>
298                 <version>${commons-fileupload.version}</version>
299             </dependency>
300             <dependency>
301                 <groupId>org.apache.httpcomponents</groupId>
302                 <artifactId>httpclient</artifactId>
303                 <version>${httpclient.version}</version>
304             </dependency>
305             <dependency>
306                 <groupId>commons-net</groupId>
307                 <artifactId>commons-net</artifactId>
308                 <version>${commons-net.version}</version>
309             </dependency>
310             <dependency>
311                 <groupId>commons-codec</groupId>
312                 <artifactId>commons-codec</artifactId>
313                 <version>${commons-codec.version}</version>
314             </dependency>
315             <!--<dependency>-->
316             <!--<groupId>org.apache.shiro</groupId>-->
317             <!--<artifactId>shiro-core</artifactId>-->
318             <!--<version>${shiro.version}}</version>-->
319             <!--</dependency>-->
320             <!--<dependency>-->
321             <!--<groupId>org.apache.shiro</groupId>-->
322             <!--<artifactId>shiro-web</artifactId>-->
323             <!--<version>${shiro.version}</version>-->
324             <!--</dependency>-->
325             <!--<dependency>-->
326             <!--<groupId>org.apache.shiro</groupId>-->
327             <!--<artifactId>shiro-cas</artifactId>-->
328             <!--<version>${shiro.version}</version>-->
329             <!--</dependency>-->
330             <!--<dependency>-->
331             <!--<groupId>org.apache.shiro</groupId>-->
332             <!--<artifactId>shiro-spring</artifactId>-->
333             <!--<version>${shiro.version}</version>-->
334             <!--</dependency>-->
335             <dependency>
336                 <groupId>org.apache.shiro</groupId>
337                 <artifactId>shiro-all</artifactId>
338                 <version>${shiro.version}</version>
339             </dependency>
340             <!--<dependency>-->
341                 <!--<groupId>org.apache.activemq</groupId>-->
342                 <!--<artifactId>activemq-all</artifactId>-->
343                 <!--<version>${activemq.version}</version>-->
344             <!--</dependency>-->
345
346
347
348
349             <!-- logback -->
350             <dependency>
351                 <groupId>org.slf4j</groupId>
352                 <artifactId>slf4j-api</artifactId>
353                 <version>${slf4j.version}</version>
354             </dependency>
355             <dependency>
356                 <groupId>org.slf4j</groupId>
357                 <artifactId>jcl-over-slf4j</artifactId>
358                 <version>${slf4j.version}</version>
359             </dependency>
360             <dependency>
361                 <groupId>ch.qos.logback</groupId>
362                 <artifactId>logback-core</artifactId>
363                 <version>${logback.version}</version>
364             </dependency>
365             <dependency>
366                 <groupId>ch.qos.logback</groupId>
367                 <artifactId>logback-classic</artifactId>
368                 <version>${logback.version}</version>
369             </dependency>
370             <dependency>
371                 <groupId>org.logback-extensions</groupId>
372                 <artifactId>logback-ext-spring</artifactId>
373                 <version>${logback.ext.version}</version>
374             </dependency>
375
376
377             <!-- 时间操作组件 -->
378             <dependency>
379                 <groupId>joda-time</groupId>
380                 <artifactId>joda-time</artifactId>
381                 <version>${joda-time.version}</version>
382             </dependency>
383
384     </dependencies>
385
386     <build>
387         <finalName>${project.artifactId}</finalName>
388         <plugins>
389             <!-- 资源文件拷贝插件 -->
390             <plugin>
391                 <groupId>org.apache.maven.plugins</groupId>
392                 <artifactId>maven-resources-plugin</artifactId>
393                 <version>2.7</version>
394                 <configuration>
395                     <encoding>UTF-8</encoding>
396                 </configuration>
397             </plugin>
398             <!-- java编译插件 -->
399             <plugin>
400                 <groupId>org.apache.maven.plugins</groupId>
401                 <artifactId>maven-compiler-plugin</artifactId>
402                 <version>3.2</version>
403                 <configuration>
404                     <source>1.7</source>
405                     <target>1.7</target>
406                     <encoding>UTF-8</encoding>
407                 </configuration>
408             </plugin>
409             <!-- 配置Tomcat插件 -->
410             <plugin>
411                 <groupId>org.apache.tomcat.maven</groupId>
412                 <artifactId>tomcat7-maven-plugin</artifactId>
413                 <version>2.2</version>
414                 <configuration>
415                     <port>8081</port>
416                     <path>/</path>
417                     <!-- 防止GET中文乱码 -->
418                     <uriEncoding>UTF-8</uriEncoding>
419                 </configuration>
420             </plugin>
421         </plugins>
422     </build>
423
424
425 </project>

spring-public.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6
7     <context:component-scan base-package="com.laolang"></context:component-scan>
8 </beans>

spring-mvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 8
 9     <context:component-scan base-package="com.laolang.km.blog.web"></context:component-scan>
10
11     <mvc:annotation-driven />
12     <mvc:default-servlet-handler />
13
14     <bean
15         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16         <property name="prefix" value="/WEB-INF/jsp/" />
17         <property name="suffix" value=".jsp" />
18     </bean>
19 </beans>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xmlns="http://java.sun.com/xml/ns/javaee"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5          version="3.0">
 6     <display-name>km-blog</display-name>
 7
 8     <welcome-file-list>
 9         <welcome-file></welcome-file>
10     </welcome-file-list>
11
12     <!-- 加载配置文件 -->
13     <context-param>
14         <param-name>contextConfigLocation</param-name>
15         <param-value>classpath:spring/spring-*.xml</param-value>
16     </context-param>
17
18
19     <!-- Spring监听器 -->
20     <listener>
21         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22     </listener>
23     <!-- 防止Spring内存溢出监听器 -->
24     <listener>
25         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
26     </listener>
27
28     <!-- 编码过滤器 仅对POST有效-->
29     <filter>
30         <filter-name>CharacterEncodingFilter</filter-name>
31         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
32         <init-param>
33             <param-name>encoding</param-name>
34             <param-value>utf-8</param-value>
35         </init-param>
36     </filter>
37     <filter-mapping>
38         <filter-name>CharacterEncodingFilter</filter-name>
39         <url-pattern>/*</url-pattern>
40     </filter-mapping>
41
42     <!-- 解决PUT请求无法提交表单数据的问题 -->
43     <filter>
44         <filter-name>HttpMethodFilter</filter-name>
45         <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
46     </filter>
47     <filter-mapping>
48         <filter-name>HttpMethodFilter</filter-name>
49         <url-pattern>/*</url-pattern>
50     </filter-mapping>
51
52     <!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 -->
53     <filter>
54         <filter-name>HiddenHttpMethodFilter</filter-name>
55         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
56     </filter>
57
58     <filter-mapping>
59         <filter-name>HiddenHttpMethodFilter</filter-name>
60         <url-pattern>/*</url-pattern>
61     </filter-mapping>
62
63     <!-- spring servlet -->
64     <servlet>
65         <servlet-name>km-blog</servlet-name>
66         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
67         <init-param>
68             <param-name>contextConfigLocation</param-name>
69             <param-value>classpath:spring/spring-mvc.xml</param-value>
70         </init-param>
71         <load-on-startup>1</load-on-startup>
72     </servlet>
73
74     <servlet-mapping>
75         <servlet-name>km-blog</servlet-name>
76         <url-pattern>/</url-pattern>
77     </servlet-mapping>
78 </web-app>

IndexController

 1 package com.laolang.km.blog.web;
 2
 3
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 import org.springframework.web.servlet.ModelAndView;
 8
 9 @Controller
10 public class IndexController {
11
12
13     @RequestMapping(value = { "/" }, method = RequestMethod.HEAD, produces = "text/html")
14     public String head() {
15         return "index";
16     }
17
18     @RequestMapping(value = { "/", "index" }, method = RequestMethod.GET, produces = "text/html")
19     public ModelAndView index() {
20         ModelAndView mv = new ModelAndView("index");
21         mv.addObject("info","hello spring mvc");
22         return mv;
23     }
24 }

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     String path = request.getContextPath();
 5     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
 6             + path + "/";
 7 %>
 8 <!DOCTYPE html>
 9 <html lang="zh-CN">
10 <head>
11 <meta charset="UTF-8">
12 <title>Title</title>
13 </head>
14 <body>
15 info:${info}
16 <br />
17 path:<%=path %>
18 <br />
19 basePath:<%=basePath %>
20
21
22 <script type="text/javascript" charset="UTF-8" src="<%=path %>/assets/js/jquery-2.1.4/jquery.min.js"></script>
23 <script type="text/javascript">
24     $(function(){
25         console.log(‘hello spring mvc!‘);
26     });
27 </script>
28 </body>
29 </html>

运行效果

加入beetl

修改spring-mvc

把jsp的配置修改为如下配置:

1 <!-- beetl 配置 -->
2 <bean id="beetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration"
3     init-method="init" />
4 <bean id="viewResolver" class="org.beetl.ext.spring.BeetlSpringViewResolver">
5     <property name="contentType" value="text/html;charset=UTF-8" />
6 </bean>

修改Controller

1 @RequestMapping(value = { "/", "index" }, method = RequestMethod.GET, produces = "text/html")
2 public ModelAndView index() {
3     ModelAndView mv = new ModelAndView("index.html");
4     mv.addObject("info","hello spring mvc");
5     return mv;
6 }

添加bettl.properties

  1 # classpath 跟路径
  2 RESOURCE.root=/WEB-INF/views
  3 # 是否检测文件变化
  4 RESOURCE.autoCheck= true
  5
  6 #######默认配置
  7 #ENGINE=org.beetl.core.engine.FastRuntimeEngine
  8 #DELIMITER_PLACEHOLDER_START=${
  9 #DELIMITER_PLACEHOLDER_END=}
 10 #DELIMITER_STATEMENT_START=<%
 11 #DELIMITER_STATEMENT_END=%>
 12 #DIRECT_BYTE_OUTPUT = FALSE
 13 #HTML_TAG_SUPPORT = true
 14 #HTML_TAG_FLAG = #
 15 #HTML_TAG_BINDING_ATTRIBUTE = var
 16 #NATIVE_CALL = TRUE
 17 #TEMPLATE_CHARSET = UTF-8
 18 #ERROR_HANDLER = org.beetl.core.ConsoleErrorHandler
 19 #NATIVE_SECUARTY_MANAGER= org.beetl.core.DefaultNativeSecurityManager
 20 #RESOURCE_LOADER=org.beetl.core.resource.ClasspathResourceLoader
 21 #MVC_STRICT = FALSE
 22 #
 23 #### 资源配置,resource后的属性只限于特定ResourceLoader ####
 24 ##classpath 跟路径
 25 #RESOURCE.root= /
 26 ##是否检测文件变化
 27 #RESOURCE.autoCheck = TRUE
 28 ##自定义脚本方法文件位置
 29 #RESOURCE.functionRoot = functions
 30 ##自定义脚本方法文件的后缀
 31 #RESOURCE.functionSuffix = html
 32 ##自定义标签文件位置
 33 #RESOURCE.tagRoot = htmltag
 34 ##自定义标签文件后缀
 35 #RESOURCE.tagSuffix = tag
 36 #
 37 ##如果采用beetl集成的web应用,可以在渲染模板前调用如下类,此类必须实现WebRenderExt接口
 38 #WEBAPP_EXT =
 39 #
 40 ##允许html function or Tag 使用特殊的定界符
 41 #FUNCTION_TAG_LIMITER=
 42 #
 43 ######  扩展 ##############
 44 ### 内置的方法
 45 #FN.date = org.beetl.ext.fn.DateFunction
 46 #FN.nvl = org.beetl.ext.fn.NVLFunction
 47 #FN.debug = org.beetl.ext.fn.DebugFunction
 48 ##兼容以前版本,用has代替
 49 #FN.exist = org.beetl.ext.fn.CheckExistFunction
 50 #FN.has = org.beetl.ext.fn.CheckExistFunction
 51 #FN.printf = org.beetl.ext.fn.Printf
 52 #FN.decode = org.beetl.ext.fn.DecodeFunction
 53 #FN.assert = org.beetl.ext.fn.AssertFunction
 54 #FN.print = org.beetl.ext.fn.Print
 55 #FN.println = org.beetl.ext.fn.Println
 56 #FN.trunc = org.beetl.ext.fn.TruncFunction
 57 ##兼容以前版本 empty,用isEmpty代替
 58 #FN.empty = org.beetl.ext.fn.EmptyFunction
 59 #FN.qmark = org.beetl.ext.fn.QuestionMark
 60 #FN.isEmpty = org.beetl.ext.fn.EmptyExpressionFunction
 61 #FN.isNotEmpty = org.beetl.ext.fn.IsNotEmptyExpressionFunction
 62 #FN.parseInt = org.beetl.ext.fn.ParseInt
 63 #FN.parseLong = org.beetl.ext.fn.ParseLong
 64 #FN.parseDouble= org.beetl.ext.fn.ParseDouble
 65 #FN.range = org.beetl.ext.fn.Range
 66 #FN.flush = org.beetl.ext.fn.Flush
 67 #FN.json = org.beetl.ext.fn.Json
 68 #FN.pageCtx = org.beetl.ext.fn.PageContextFunction
 69 #FN.type.new=org.beetl.ext.fn.TypeNewFunction
 70 #FN.type.name=org.beetl.ext.fn.TypeNameFunction
 71 #FN.global=org.beetl.ext.fn.DynamicGlobalValueFunction
 72 #
 73 ###内置的功能包
 74 #FNP.strutil = org.beetl.ext.fn.StringUtil
 75 #FNP.reg = org.beetl.ext.fn.RegxFunctionUtil
 76 #
 77 #FNP.array = org.beetl.ext.fn.ArrayUtil
 78 #
 79 ###内置的格式化函数
 80 #FT.dateFormat =  org.beetl.ext.format.DateFormat
 81 #FT.numberFormat =  org.beetl.ext.format.NumberFormat
 82 ###内置的默认格式化函数
 83 #FTC.java.util.Date = org.beetl.ext.format.DateFormat
 84 #FTC.java.sql.Date = org.beetl.ext.format.DateFormat
 85 #FTC.java.sql.Time = org.beetl.ext.format.DateFormat
 86 #FTC.java.sql.Timestamp = org.beetl.ext.format.DateFormat
 87 #FTC.java.lang.Short = org.beetl.ext.format.NumberFormat
 88 #FTC.java.lang.Long = org.beetl.ext.format.NumberFormat
 89 #FTC.java.lang.Integer = org.beetl.ext.format.NumberFormat
 90 #FTC.java.lang.Float = org.beetl.ext.format.NumberFormat
 91 #FTC.java.lang.Double = org.beetl.ext.format.NumberFormat
 92 #FTC.java.math.BigInteger = org.beetl.ext.format.NumberFormat
 93 #FTC.java.math.BigDecimal = org.beetl.ext.format.NumberFormat
 94 #FTC.java.util.concurrent.atomic.AtomicLong = org.beetl.ext.format.NumberFormat
 95 #FTC.java.util.concurrent.atomic.AtomicInteger = org.beetl.ext.format.NumberFormat
 96 #
 97 ###虚拟属性 无
 98 ### 标签类
 99 #TAG.include= org.beetl.ext.tag.IncludeTag
100 #TAG.includeFileTemplate= org.beetl.ext.tag.IncludeTag
101 #TAG.layout= org.beetl.ext.tag.LayoutTag
102 #TAG.delete= org.beetl.ext.tag.DeleteTag
103 #TAG.htmltag= org.beetl.ext.tag.HTMLTagSupportWrapper
104 #TAG.htmltagvar= org.beetl.ext.tag.HTMLTagVarBindingWrapper
105 #TAG.cache= org.beetl.ext.tag.cache.CacheTag

在WEB-INF/views下添加index.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 info:${info}
 9
10 <script type="text/javascript" charset="UTF-8" src="/assets/js/jquery-2.1.4/jquery.min.js"></script>
11 <script type="text/javascript">
12     $(function(){
13         console.log(‘hello spring mvc!‘);
14
15     });
16 </script>
17 </body>
18 </html>

加入logback【日志文件有重复输出】

添加logback.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2
 3 <configuration>
 4
 5     <property name="LOG_HOME" value="c:/projectpath/km/km-blog/log"/>
 6     <property name="LOG_FILE_NAME" value="km-blog-log"/>
 7
 8     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
 9         <!-- encoder的默认实现类是ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
10         <encoder>
11             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -[%file]:[%line] - %msg%n</pattern>
12         </encoder>
13     </appender>
14
15     <appender name="pullLog"
16               class="ch.qos.logback.core.rolling.RollingFileAppender">
17         <file>${LOG_HOME}/${LOG_FILE_NAME}.log</file>
18
19         <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
20             <fileNamePattern>${LOG_HOME}/${LOG_FILE_NAME}.%i.log.gz</fileNamePattern>
21             <!-- 当归档文件超过三个后,覆盖最早的日志 -->
22             <minIndex>1</minIndex>
23             <maxIndex>500</maxIndex>
24         </rollingPolicy>
25
26         <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
27             <!-- 当文件超过500KB时进行压缩 -->
28             <maxFileSize>10MB</maxFileSize>
29         </triggeringPolicy>
30         <encoder>
31             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -[%file]:[%line] - %msg%n</pattern>
32         </encoder>
33     </appender>
34
35     <!-- mybatis show sql -->
36     <logger name="dao" level="DEBUG" additivity="true">
37         <appender-ref ref="pullLog" />
38     </logger>
39
40     <!-- name值可以是包名或具体的类名:该包(包括子包)下的类或该类将采用此logger
41         additivity 设置为true可在控制台看到pulllog日志,但是文件中会输出两次,设置为false文件输出正常,控制台看不到日志
42     -->
43     <logger name="com.laolang" level="INFO" additivity="true">
44         <appender-ref ref="pullLog"/>
45     </logger>
46
47     <!-- root的默认level是DEBUG -->
48     <root level="DEBUG">
49         <appender-ref ref="STDOUT"/>
50         <appender-ref ref="pullLog"/>
51     </root>
52 </configuration>

在Controller中使用

 1 package com.laolang.km.blog.web;
 2
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.servlet.ModelAndView;
 9
10 @Controller
11 public class IndexController {
12
13     Logger log = LoggerFactory.getLogger(IndexController.class);
14
15     @RequestMapping(value = { "/" }, method = RequestMethod.HEAD, produces = "text/html")
16     public String head() {
17         return "index";
18     }
19
20     @RequestMapping(value = { "/", "index" }, method = RequestMethod.GET, produces = "text/html")
21     public ModelAndView index() {
22         log.info("打开首页");
23         ModelAndView mv = new ModelAndView("index.html");
24         mv.addObject("info", "hello spring mvc");
25         return mv;
26     }
27 }

控制台输出

文件输出

输出json

User.java

 1 package com.laolang.km.blog.domain;
 2
 3 import java.util.Date;
 4
 5 import com.fasterxml.jackson.annotation.JsonFormat;
 6
 7 public class User {
 8
 9         // 省略 getter,setter,toString,构造方法
10
11     private Long id;
12
13     private String uNum;
14
15     private String nickName;
16
17     @JsonFormat(pattern = "yyyy-MM-dd")
18     private Date birthday;
19
20     private String blogHome;
21
22     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
23     private Date created;
24
25     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
26     private Date updated;
27 }

controller

 1 package com.laolang.km.blog.web;
 2
 3 import java.util.Date;
 4
 5 import org.springframework.http.ResponseEntity;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.PathVariable;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.bind.annotation.RequestMethod;
10
11 import com.laolang.km.blog.domain.User;
12
13 @RequestMapping("user")
14 @Controller
15 public class UserController {
16
17     @RequestMapping(value="id/{id}",method=RequestMethod.GET,produces="application/json; charset=utf-8")
18     public ResponseEntity<User> findById(@PathVariable("id") Long id){
19
20         User user = new User();
21         user.setBirthday(new Date());
22         user.setCreated(new Date());
23         user.setUpdated(user.getCreated());
24
25         if( null == id || 1L == id ) {
26             user.setBlogHome("http://km.blog.com/userOne");
27             user.setId(1L);
28             user.setuNum("u1001");
29             user.setNickName("xiaodaima");
30         }else if( 2L == id ) {
31             user.setBlogHome("http://km.blog.com/userOne");
32             user.setId(1L);
33             user.setuNum("u1001");
34             user.setNickName("xiaodaima");
35         }
36
37         return ResponseEntity.ok(user);
38     }
39 }

postman测试

原文地址:https://www.cnblogs.com/khlbat/p/8193893.html

时间: 2024-10-13 07:49:26

km之路--009 spring mvc 起手的相关文章

Spring MVC源码——Root WebApplicationContext

目录 Spring MVC源码--Root WebApplicationContext 上下文层次结构 Root WebApplicationContext 初始化和销毁 ContextLoaderListener ContextLoader Servlet 3.0+ 中初始化 参考资料 Spring MVC源码--Root WebApplicationContext 打算开始读一些框架的源码,先拿 Spring MVC 练练手,欢迎点击这里访问我的源码注释. Spring MVC 的文档一开始

利用maven构建一个spring mvc的helloworld实例

刚开始学习maven和spring mvc,学的云里雾里的 这里提供一个hello world实例,记录自己的学习之路 首先看maven官网的介绍 Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and do

基于Spring Boot构建的Spring MVC快速入门

原文地址:http://tianmaying.com/tutorial/spring-mvc-quickstart 环境准备 一个称手的文本编辑器(例如Vim.Emacs.Sublime Text)或者IDE(Eclipse.Idea Intellij) Java环境(JDK 1.7或以上版本) Maven 3.0+(Eclipse和Idea IntelliJ内置,如果使用IDE并且不使用命令行工具可以不安装) 一个最简单的Web应用 使用Spring Boot框架可以大大加速Web应用的开发过

使用Spring Mvc 转发 带着模板 父页面 之解决方法 decorators.xml

周末了,周一布置的任务还没完成,卡在了页面跳转上,接手了一个半截的项目要进行开发,之前没有人给培训,全靠自己爬代码,所以进度比较慢,而且加上之前没有用过 Spring Mvc 开发项目,所以有点吃力,不过接触了Spring Mvc近一个月的时间感觉 开发速度确实比 SSH快不少,不用一个一个的Bean去配置,直接扫描就OK了,可就是这样还是有些地方容易搞上一天也没搞多少进度,这不,被我新写的一个 Controller 的转发搞晕了,我本来要实现一个列表的分页查询,哪里想到点下一页的时候,除了我要

SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现

到这里已经写到第12篇了,前11篇基本上把Spring MVC主要的内容都讲了,现在就直接上一个项目吧,希望能对有需要的朋友有一些帮助. 一.首先看一下项目结构: InfrastructureProjects:是抽取出的基础项目,主要封装了一些通用的操作. SpringMVC3Demo:就是管理系统所在的项目. WeiXinAPI:是之前做微信管理平台测试时封装一些操作,如果不需要把该项目移除即可. 注:项目的前端UI框架用的是国外的一个基于Bootstrap框架的开发的Demo,如不需要替换为

spring mvc 与Struts的认识

首先,纠正一个概念上的错误.ssh一般意义上是指 struts,spring framework以及hibernate.这三个框架作用是不一样的.hibernate主要是用于持久层,struts主要是用于mvc,而spring主要用于aop和ioc.再来看 spring mvc.从名字来看,就知道这是一个mvc框架,所以,spring mvc和ssh根本就没有可比性,他们不是一个东西.spring mvc和struts都是mvc框架,他俩才有比较的意义.回到正题,spring mvc和strut

Spring+Spring MVC+Hibernate环境搭配

Spring+Spring MVC+Hibernate简称"SSH".Spring容器是Spring的核心,该 容器负责管理spring中的java组件.Spring的核心机制:依赖注入.Hibernate是一个不错的ORM(关系对象映射)框架.Spring+Spring MVC+Hibernate环境搭配步骤: 1.搭建Spring+Hibernate环境(跟ssh搭建步骤一致) (1)加入Spring+Hibernate的架包. 2.搭建SpringMVC环境( 1)添加Sprin

SI 系列入门 之 手把手构建 Spring MVC 工程

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. SI 前文有述,Spring MVC + MyBatis 的简称,因为 Spring MVC 内部已有注入功能,故不考虑其是否是利用了 Sprin

spring mvc+ELK从头开始搭建日志平台

spring mvc+ELK从头开始搭建日志平台 最近由于之前协助前公司做了点力所能及的事情,居然收到了一份贵重的端午礼物,是给我女儿的一个乐高积木,整个有7大包物件,我花了接近一天的时间一砖一瓦的组织起来,虽然很辛苦但是能够从过程中体验到乐趣.这次将分享从头搭建分布式日志系统,主要是在spring mvc上结合ELK套件实现(之前有些工作由于分工不同由不同的同事来完成,我只是在已经配置好的环境下做开发而已),包含如下这些技术点: spring mvc logback logstash elas