TAG

RSS订阅

收藏本站

设为首页

当前位置:主页 > 服务端开发 > Java开发 > Spring开发学习 >

SpringMVC 注解配置

发布时间:2015-11-16 14:37 类别:Spring开发学习

用spring mvc 已经有一段时间了,感觉真的比struts2好用(个人认为)...

下面写一篇,基于注解配置的springmvc小教程(hello world)

demo演示:http://demo.mkfree.com/springmvc/helloworld/index

springMvc 注解配置例子(hello world)

简介

spring mvc 是一个web application 一个标准的mvc (model,views ,controller)框架

spring mvc 的基本概念,看下图,就已经非常清晰了

处理请求的基本流程

 

Spring MVC中,核心disatcher组件是“的DispatcherServlet”,这作为前端控制器(设计模式)。每个Web请求去通过这种“DispatcherServlet的”,“DispatcherServlet的”将Web请求调度到适当的处理程序。
小例子演示

 

环境配置:

 

  • spring 3.1.2 release
  • jdk 1.7
  • maven 3

 

目录结构

好了,下面我们看看搭配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


猜你会喜欢....

Copyright © 2015 www.wahenzan.com 哇!很赞 版权所有 浙ICP备14030256号-1 Power by DedeCms

浙公网安备 33010602005986号

声明:本站所有文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除 网站管理员:758763728

360网站安全检测平台