第一个配置SpringMVC,HelloWorld程序
来源:互联网 发布:秒装软件下载 编辑:程序博客网 时间:2024/05/15 05:37
1、引用SpringMVC包,在pom.xml文件dependencies元素内添加
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.5.RELEASE</version> </dependency>
2、配置DispatcherServlet类,在web.xml添加
所有的请求都经过DispatcherServlet转发对应Controller中匹配RequestMapping值的方法处理
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
url-pattern值:为/ 不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类。为/*会匹配*.jsp,会出现返回jsp视图时进入spring的DispatcherServlet 类,导致找不到对应的RequestMapping报404错
3、在WEB-INF目录下创建springmvc-servlet.xml文件
content:component-scan 用来扫描包下注解来创建bean。DispatcherServlet 在初始化时,Spring MVC 会查找 web 应用 WEB_INF 目录下的[servlet-name]-servlet.xml 并创建在此文件定义的 bean
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.java.web"/></beans>
4、创建HelloController
@Controller是标记该类为Controller。没有实际作用。主要让Spring创建该类对象,不需要去beans文件中配置@RequestMapping 配置处理URL@ResponseBody 表示方法返回的值,会写入Response的body中
@Controllerpublic class HelloController { @RequestMapping(value = "/hello") @ResponseBody public String hello(){ return "hello world"; }}
a、在pom.xml文件中build元素内添加Jetty插件包<plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.14.v20161028</version> <configuration> <scanIntervalSeconds>1</scanIntervalSeconds> </configuration> </plugin> </plugins>b、配置Jetty
Working directory:指定项目目录路径Command line:指定Jetty命令c、运行程序
6、访问
0 0
- 第一个配置SpringMVC,HelloWorld程序
- springmvc第一个项目helloworld配置
- 第一个helloworld程序
- 第一个程序:HelloWorld
- 第一个程序HelloWorld
- 第一个程序:HelloWorld
- SpringMVC 的第一个程序——HelloWorld
- Android开发环境配置及运行第一个程序Helloworld
- 第一个JAVA程序 helloworld
- 第一个FreeMarker HelloWorld程序
- 第一个android程序:helloworld
- 第一个android程序--helloworld
- Android第一个程序"HelloWorld"
- 第一个JAVA程序HelloWorld
- 第一个Android程序HelloWorld
- log4cpp第一个程序HelloWorld
- 第一个servlet程序---helloworld
- IOS第一个程序helloworld
- RecyclerView 多布局,Item中使用EditText刷新问题扫坑
- BZOJ 4003 左偏树
- Jquery操作select选项集合!
- Nginx启动报error while loading shared libraries: libpcre.so.1异常
- 几个Java编程的典型例子
- 第一个配置SpringMVC,HelloWorld程序
- 好人
- 一个sphere mapping的简单应用实例
- 更改Xcode的缺省公司名
- 模糊系统与模糊控制教程--学习记录
- JSP - 概述
- 【Demo 记录】获取栈顶APP所在的应用包名 --getRunningTasks 弃用的处理方案
- React-Native -- 可继承的Navigator
- 学习小记-Flask Web (2) 尝试用MySQL代替SQLite