oyhk 學習筆記 html
用spring mvc 已經有一段時間了,感受真的比struts2好用(我的認爲)... 前端
下面寫一篇,基於註解配置的springmvc小教程(hello world) java
demo演示:http://demo.mkfree.com/springmvc/helloworld/index web
springMvc 註解配置例子(hello world) spring
簡介 apache
spring mvc 是一個web application 一個標準的mvc (model,views ,controller)框架 設計模式
spring mvc 的基本概念,看下圖,就已經很是清晰了 api
處理請求的基本流程 spring-mvc
Spring MVC中,核心disatcher組件是「的DispatcherServlet」,這做爲前端控制器(設計模式)。每一個Web請求去經過這種「DispatcherServlet的」,「DispatcherServlet的」將Web請求調度到適當的處理程序。小例子演示
環境配置: mvc
目錄結構
好了,下面咱們看看搭配spring mvc jsp 註解例子須要依賴的jar包,我用了maven 管理起來比較方便,下面是pom.xml的代碼
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>free-demo</artifactId> <groupId>free-demo</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>free-demo</groupId> <artifactId>springmvc_helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springVersion}</version> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project>controller代碼
package com.mkfree.demo.controller; 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 public class HelloworldController { @RequestMapping(value = "/", method = RequestMethod.GET) public String index(Model model) { return "index"; } @RequestMapping(value = "/springmvc/helloworld", method = RequestMethod.GET) public String helloworld(Model model) { model.addAttribute("msg", "springmvc helloworld例子演示成功啦..."); return "helloworld"; } }index.jsp
<%@page pageEncoding="UTF-8" language="java" contentType="text/html; charset=utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <body> <h1>這裏是一個springmvc的例子</h1> <a href="/springmvc/helloworld">springmvc/helloworld</a> </body> </html>helloworld.jsp
<%@page pageEncoding="UTF-8" language="java" contentType="text/html; charset=utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <body> <h1>這裏是一個springmvc的例子</h1> <h2>${msg}</h2> </body> </html>spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!--註解說明 --> <context:annotation-config /> <!-- 把標記了@Controller註解的類轉換爲bean --> <context:component-scan base-package="com.mkfree.demo.controller" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="free-blog" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>free-front</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-servlet.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>free-front</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- 訪問ico時,交給默認的servlet處理 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.ico</url-pattern> </servlet-mapping> </web-app>以上就是一個springmvc 例子,但願對學者有用若是有興趣者能夠下載源代碼
源代碼下載:free-demo.rar