SpringMVC学习(1):HelloWorld
来源:互联网 发布:电脑截图软件下载 编辑:程序博客网 时间:2024/05/21 20:29
在学习了Spring框架的基础上再进行SpringMVC的学习是一件比较简单的事情,下面开始讲一下如何写一个SpringMVC的HelloWorld
〇、开始前的准备
IDE工具Eclipse,Eclipse中最好已经装了Spring插件,PC机中部署了Tomcat服务器(最好是7.0版本的,8.0版本在使用HiddenHttpMethodFilter时会进行保护而出错)
一、新建一个项目
新建一个Dynamic Web Project,完整的工程的目录如下:
二、添加依赖的jar包
SpringMVC需要用到的jar包有以下这些:
我使用的是MAVEN的方式添加
<properties><!-- Generic properties --><java.version>1.6</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- Spring --><spring-framework.version>4.3.7.RELEASE</spring-framework.version></properties> <!-- add dependencies --> <dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-framework.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-framework.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring-framework.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-web --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-framework.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version></dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring-framework.version}</version></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version></dependency> </dependencies>
值得注意的一点是,如果你是自己添加包,那么添加到WebContent->WEB-INF->lib中
如果你是使用MAVEN添加,需要右键项目->Properties->Deployment Assembly->Add
把这个配置进去
否则无法在服务器上成功运行,大概会报错例如ClassNotFoundException或者ClassNotDefException。
三、配置DispatcherServlet
在web.xml文件中配置DispatcherServlet,如果你忘了在新建文件时勾选生成web.xml文件,可以右键项目->Java EE Tools->Generate Deployment Descriptor Stub生成一个。
如果你已经在Eclipse中安装了Spring插件,那么在web.xml文件中可以使用Alt+/ 快捷生成配置模板
配置好的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置DispatcherServlet --><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置和名称 --><!-- 实际上也可以不通过contextConfigLocation来配置SpringMVC的配置文件,而使用默认的,默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>三、写一个HelloWorld类
这个没什么好说的,new 一个package为springmvc,然后再new一个HelloWorld类
package springmvc;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/springmvc")public class HelloWorld {private static final String SUCCESS = "success";@RequestMapping("/helloworld")public String helloWorld() {System.out.println("hello world");return SUCCESS;}}注解@Controller声明了这个类是用作控制器的
注解@RequestMapping("/springmvc")是提供初步的请求映射信息。相对于WEB应用的根目录
注解@RequestMapping("/helloworld")是提供进一步的细分映射信息。相对于类定义处的URL,若类定义处未标记@RequestMapping,则方法处标记的RUL相对于WEB应用的根目录
返回了一个String:success可以通过InternalResourceViewResolver解析为实际的视图
四、在bean配置文件中配置视图解析器
新建一个Spring Bean Configure File,然后配置自动扫描包,和视图解析器
<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="springmvc"></context:component-scan><!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"></property><property name="suffix" value=".jsp"></property></bean></beans>五、编写jsp文件
在WebContent文件夹下新建一个JSP文件:index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SpringMVC</title></head><body><a href="springmvc/helloworld">Hello World!</a></body></html>在WebContent/WEB-INF/views文件夹下新建一个JSP文件:success.jsp至于为什么是在这个文件夹,因为刚刚在视图解析器中我们定义了视图文件的路径的前缀是
value="/WEB-INF/views/"
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>SpringMVC</title></head><body><h4>success page</h4></body></html>然后右键index.jsp,run as->run on server就可以看到效果了。
可以看到页面跳转正确,在控制台中也成功输出了hello world
- SpringMVC学习(1):HelloWorld
- springmvc学习笔记 1 --- helloworld
- SpringMVC学习(一)-HelloWorld
- SpringMVC框架学习(1)——HelloWorld
- SpringMVC学习之helloworld(一)
- SpringMVC学习(一)之HelloWorld
- SpringMVC学习(1.helloworld)
- SpringMVC学习第一章:HelloWorld
- springmvc学习笔记(1)——搭建springmvc项目之HelloWorld
- 学习SpringMVC(一)之helloworld
- SpringMVC学习简单HelloWorld实例
- SpringMVC -- HelloWorld/@RequestMapping(1)
- SpringMVC-1 helloworld
- SpringMVC-1,helloworld
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- springmvc学习笔记(一)第一个helloworld
- eclipse安装教程、如何安装java环境、ndk安装教程、
- xamarin android Fragment实现底部导航栏
- Python 类方法,实例方法,类变量,实例变量
- hive count(*) group by 坑
- JAVA 反射
- SpringMVC学习(1):HelloWorld
- JavaCV安装过程
- [LeetCode]169. Majority Element(多数元素)
- PE文件详解(一)
- MPI并行编程计算π值及PSRS排序
- Spark SQL数据加载和保存实战
- python机器学习代码下载(书上的)
- Caffe源码解读(十):Caffe五种层的实现和参数配置
- Springmvc视图解析器InternalResourceViewResolver问题分析