您的当前位置:首页正文

Springmvc零基础入门

2021-06-07 来源:易榕旅网
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

springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml springmvc / 在源代码主目录下src/main/java/加入springmvc-servlet.xml

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\">

class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\"

id=\"internalResourceViewResolver\">

加入WEB依赖和json依赖。

org.springframework spring-web 4.3.7.RELEASE

org.springframework spring-webmvc 4.3.7.RELEASE

com.fasterxml.jackson.core jackson-databind 2.8.5

添加类

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\") publicclass 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\"); returnmodelAndView; }

@RequestMapping(\"/hello2\") @ResponseBody

public HashMap hello2() { HashMapmap = new HashMap(); map.put(\"kkkk\", \"vvvv\"); returnmap; } }

添加JSP页面到WebContent/WEB-INF/jsp/hello.jsp

配置启动服务。

访问地址:如下,说明成功。

因篇幅问题不能全部显示,请点此查看更多更全内容