SpringMVC教程--Idea中使用Maven创建SpringMVC项目
2024-09-04 02:55:58
1、新建项目
参照idea教程中的创建maven项目https://www.cnblogs.com/daxiang2008/p/9061653.html
2、POM中加入依赖包
(1)指定版本
(2)配置依赖
3、配置web.xml
4、创建springMVC.xml
配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称,实际上也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件, 而使用默认的.
其中servlet名是任意的,但是springmvc配置文件名是有要求的必须为,默认文件名为: <servlet-name>-servlet.xml
如:该文件名必须为: spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- 不操作静态资源 -->
<mvc:default-servlet-handler/> <!-- 启用springmvc注解 (springmvc必须配置,用于@RequestMapping结合使用) -->
<mvc:annotation-driven/> <!-- 扫描所有的Controller 注:spring mvc容器只负责控制端处理 -->
<context:component-scan base-package="com.daxiang.kb.controler"/> <!-- 配置视图解析, InternalResourceViewResolver:视图名称解析器。 在这里prefix和suffix分别表示查找视图页面的前缀和后缀,
比如传进来的逻辑视图名为hello,那么该jsp视图页面应该存放在”/WEB-INF/jsp/hello.jsp” -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property><!-- 前缀 -->
<property name="suffix" value=".jsp"></property><!-- 后缀 -->
</bean> </beans>
5、创建控制器
/**
* Copyright (C), 2018-2018,
* FileName: Query
* Author: 大象
* Date: 2018-09-17 14:32
*/
package com.daxiang.kb.controler; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; /**
* 〈〉<br>
*
* @author daxiang
* @create 2018-09-17
* @since 1.0.0
*/
@Controller
public class Query {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("greeting", "Hello Spring MVC");
return "index"; }
}
最新文章
- Bubble Cup 8 finals I. Robots protection (575I)
- 内存提取SWF,破解doswf方法概述
- C#通过事件跨类调用WPF主窗口中的控件
- DOM何时Ready
- XMPP客户端开发(2)--发送接收消息
- 走着官方的教程入门Material Design(一)
- 《Java程序设计》第十周学习总结
- WPF处理Windows消息
- [Redux] Supplying the Initial State
- 关于DLL中Resources文件修改
- SQL Server MySQL 中的 in 与 null
- ASP.NET CS文件中输出JavaScript脚本
- java类中为什么设置set和get方法操作属性
- MySQL备份说明
- 小试ImageMagik——使用篇
- ASP.NET的版本?
- 第十五周翻译-《Pro SQL Server Internals, 2nd edition》
- [django]session设置与获取原理
- HTTP与TCP的区别和联系--转载
- java 基本数据类型及自动类型提升