以前複製粘貼建立了幾個ssm的項目,然而回頭讓本身寫的時候仍是一頭霧水,究其緣由是spring的陌生。僅僅是寫過幾個helloworld而已。並且是照着寫。我都不知道springmvc到底須要多少jar,都用來幹嗎。因此,接下來要用心看spring原理了。css
最近因爲有定時任務的需求,簡單搭建了一個springmvc4的helloworld。html
ide採用的是idea,固然eclipse應該也是沒問題的,由於都是maven項目。前端
能夠經過new->project->maven來建立。建立完後補充成正常的javaweb結構。即src\main\java,src\main\resource,src\main\webapp.java
關於jar包有多少,幹啥用,還不清楚。這裏是我在網上找的一個版本,不多jar,之後須要的時候逐漸增長就好了。git
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>spmvtest</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>spmvtest Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 設定主倉庫,按設定順序進行查找。 --> <repositories> <!-- 若有Nexus私服, 取消註釋並指向正確的服務器地址. <repository> <id>nexus-repos</id> <name>Team Nexus Repository</name> <url>http://192.168.11.36:8888/nexus/content/groups/public</url> </repository> --> <repository> <id>oschina-repos</id> <name>Oschina Releases</name> <url>http://maven.oschina.net/content/groups/public</url> </repository> <repository> <id>java-repos</id> <name>Java Repository</name> <url>http://download.java.net/maven/2/</url> </repository> <repository> <id>springsource-repos</id> <name>SpringSource Repository</name> <url>http://repo.spring.io/release/</url> </repository> <repository> <id>central-repos</id> <name>Central Repository</name> <url>http://repo.maven.apache.org/maven2</url> </repository> <repository> <id>central-repos2</id> <name>Central Repository 2</name> <url>http://repo1.maven.org/maven2/</url> </repository> </repositories> <!-- 設定插件倉庫 --> <pluginRepositories> <!-- 若有Nexus私服, 取消註釋並指向正確的服務器地址. <pluginRepository> <id>nexus-repos</id> <name>Team Nexus Repository</name> <url>http://192.168.11.36:8888/nexus/content/groups/public</url> </pluginRepository> --> <pluginRepository> <id>oschina-repos</id> <name>Oschina Releases</name> <url>http://maven.oschina.net/content/groups/public</url> </pluginRepository> </pluginRepositories> <properties> <spring.version>4.0.8.RELEASE</spring.version> <quartz.version>2.2.1</quartz.version> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!--springmvc begin--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!--springmvc end--> <!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>${quartz.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> </dependencies> <build> <finalName>spmvtest</finalName> </build> </project>
設置攔截器和過濾器github
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springmvc前端控制器,rest配置 --> <servlet> <servlet-name>springmvc_rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等) 若是不配置contextConfigLocation,默認加載的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc_rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- post亂碼過慮器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--靜態資源直接訪問--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.ico</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> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.test"/> <!-- 計劃任務配置,用 @Service @Lazy(false)標註類,用@Scheduled(cron = "0 0 2 * * ?")標註方法 --> <task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/> <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <context:component-scan base-package="com.test"/> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
package com.test.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by Administrator on 2016/4/14 0014. */ @Controller public class HelloController { @RequestMapping("/index") public String index(ModelMap modelMap){ modelMap.put("username", "Ryan miao"); return "index"; } }
<html> <body> <h2>Hello World!</h2> hello ${username} </body> </html>
package com.test.task; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; /** * Created by Administrator on 2016/4/14 0014. */ @Service @Lazy(false) public class MyTask implements ITask { private static int i = 0; private static int y = 0; @Scheduled(cron="0/5 * * * * ?") //每5秒執行一次 public void testCyc() { System.out.println("task test,執行次數:"+i++); } /** * 心跳更新。啓動時執行一次,以後每隔2秒執行一次 */ @Scheduled(fixedRate = 1000*2) public void print(){ System.out.println("心跳執行次數:"+y++); } }
9.測試代碼:githubweb