标签:springmvc+mybatis dubbo+zookeeper restful redis分布式缓存 kafka
本例子为你讲解在spring3中如何使用基于注解的mvc框架.
例子中使用到的工具:
MyEclipse 9.1
jdk 1.6
1.添加Jar包引用
由于使用了Maven管理项目,所以,第一步就是添加引用.(没有使用Maven怎么办?那你直接下载Spring3的压缩包,添加相应的Jar文件就可以了。)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.1.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
2.编写 Controller 和 Mapping
我们采用注解的方式配置,如果想使用XML的方式,可以查看文档,都是一样的配制方法.
package com.vito.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
3.JSP视图
<html>
<body>
<h1>Message : ${message}</h1>
</body>
</html>
4.Spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.vito.action" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 -->
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
5.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">
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.成果
访问:minglisoft.cn/technology
就可以得到这样的画面了:
本站所有代码来源请查看:minglisoft.cn/technology
标签:springmvc+mybatis dubbo+zookeeper restful redis分布式缓存 kafka
原文地址:http://12822360.blog.51cto.com/12812360/1915641