spring官网:http://projects.spring.io/spring-framework/Eclips新建一项目NEW->Other…输入“maven”选择Maven Project,点击Next。一路默认,如下图,输入”com.test”,”smvc”,Finish。项目上点击右键,properties,选择“Project Facets”。点击“Convert to faceted from…”,选择“Dynamic Web Module”。OK。官网上的maven依赖拷贝过来。项目上点击右键,properties,选择“Deployment Assembly”。点击Add…选择Java Build Path Entries,选择Maven Dependencies。Finish在WebContent/WEB-INF里添加web.xml<?xml version="1.0"encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee"xmlns:web_1="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><!--configure the setting of springmvcDispatcherServlet and configure the mapping--><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:springmvc-servlet.xml</param-value></init-param><!--<load-on-startup>1</load-on-startup>--></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>在源代码主目录下src/main/java/加入springmvc-servlet.xml<?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: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/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!--scan the package and the sub package--><context:component-scan base-package="com.test.control"/><!--don’t handle the static resource--><mvc:default-servlet-handler/><!--if you use annotation you must configure following setting--><mvc:annotation-driven/><!--configure the InternalResourceViewResolver--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!--前缀--><property name="prefix"value="/WEB-INF/jsp/"/><!--后缀--><property name="suffix"value=".jsp"/></bean></beans>加入WEB依赖和json依赖。<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.7.RELEASE</version></dependency><!--Jackson JSON Processor--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.5</version></dependency>添加类package com.test.smvc.control;import java.util.Date;import java.util.HashMap;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/mvc")public class Test{@RequestMapping("/hello")public String hello(){return"hello";}@RequestMapping("/hello1")public ModelAndView hello1(){ModelAndView modelAndView=new ModelAndView();modelAndView.addObject("name",new Date());modelAndView.addObject("namexx",new Object());modelAndView.setViewName("hello");return modelAndView;}@RequestMapping("/hello2")@ResponseBody public HashMap<String,String>hello2(){HashMap<String,String>map=new HashMap<String,String>();map.put("kkkk","vvvv");return map;}}添加JSP页面到WebContent/WEB-INF/jsp/hello.jsp配置启动服务。访问地址:如下,说明成功。(注:可编辑下载,若有不当之处,请指正,谢谢!)。
""""""此处省略40%,请
登录会员,阅读正文所有内容。