Java SpringMVC学习--基础配置
快速开始一个基于SpringMVC框架的web项目
开发工具 Eclipse neon.2
运行环境 tomcat8.5
1.在Eclipse中新建一个web项目:File-New-Dynamic Web Project
输入项目名称,Target runtime这里选择已经配置好的tomcatv8.5,Dynamic web module version 选择2.5来自定义web.xml。 Finish

2.新建好项目后,将spring相关jar包直接复制到WebContent-Web-INF-lib目录下.
官网链接:http://repo.springsource.org/libs-release-local/org/springframework/spring/ 选择相应版本即可。
除了从spring官方网站上下载的jar包外,还需要额外添加standard.jar;jstl.jar;commons-logging.jar这三个包.
3.在WebContent-Web-web.xml中配置前端控制器. 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Springtest</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置dispatcherservlet-->
<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:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--配置字符过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
4.在src包下,新建xml文件,命名为spring-mvc.xml。代码如下,其中base-package为自己项目的包名
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.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-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.amazon"/>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean> </beans>
5.在项目包内新建class,命名为testcontroller.java. 请求路径为/test/hello,跳转到hello.jsp,并向ModelAndView中添加了一个string对象
package com.amazon.springtest.controller; import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/hello")
public ModelAndView test() {
String displayString = "helloworld!";
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teststring", displayString);
modelAndView.setViewName("hello");
return modelAndView;
} }
6.在Web-content下的index.jsp 添加一个链接,会根据请求路径,找到Controller中的test()方法。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test/hello.do">spring mvc</a>
</body>
</html>
在WEB-INF/jsp目录下的hello.jsp, 采用EL表达式输出在controller中添加的 teststring.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${teststring}
</body>
</html>
7.运行项目,点击spring mvc,返回hello world


Java SpringMVC学习--基础配置的更多相关文章
- Java 中 JDBC 基础配置
Java 中 JDBC 基础配置 <resource auth="Container" driverclassname="oracle.jdbc.driver.Or ...
- SpringMVC最基础配置
SpringMVC和Struts2一样,是前后台的一个粘合剂,struts2用得比较熟悉了,现在来配置一下SpringMVC,看看其最基础配置和基本使用.SpriingMVC不是太难,学习成本不高,现 ...
- SpringMvc的基础配置<一>
SpringMVC学习 1.此篇博文是学习以下博文,并通过亲测得来: 1.1.网址:http://www.cnblogs.com/bigdataZJ/p/springmvc1.html 2.所用软 ...
- SpringMvc学习---基础知识考核
SpringMVC 1.SpringMVC的工作流程 流程 : 1.用户发送请求至前端控制器DispatcherServlet2.DispatcherServlet收到请求调用HandlerMappi ...
- SpringMVC的基础配置及视图定位
概要 记录一下搭建SpringMVC框架的步骤 视图定位也就是改变jsp在项目中的路径 一.新建javaweb项目springmvc1,在lib中导入jar包 此项目上传了GitHub,方便去下载ja ...
- 本地Java大数据环境基础配置(Maven)
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6812623309138559500/ 创建项目 准备pom.xml文件配置(附在文档最后) 在下载jar过程中极其 ...
- java项目其他基础配置
创建完maven项目之后. 1.pom.xml文件配置项目相关的架包. 2.src.main.resources下边 创建文件夹:spring以及mapper. 3.src.main.resource ...
- 分布式项目开发-springmvc.xmll基础配置
基础步骤: 1 包扫描 2 驱动开发 3 视图解析器 4 文件上传解析器 5 拦截器 6 静态资源 <beans xmlns="http://www.springframework.o ...
- Java 系统学习梳理_【All】
Java基础 1. Java学习---JDK的安装和配置 2. Java学习---Java代码编写规范 2. Java学习---HashMap和HashSet的内部工作机制 3. Java学习---J ...
随机推荐
- springboot+redis实现缓存数据
在当前互联网环境下,缓存随处可见,利用缓存可以很好的提升系统性能,特别是对于查询操作,可以有效的减少数据库压力,Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存 ...
- MySQL PRIMARY KEY 和 UNIQUE的区别
primary key = unique + not null unique 就是唯一,当你需要限定你的某个表字段每个值都唯一,没有重复值时使用.比如说,如果你有一个person 表,并且表中有个身 ...
- xamarin RunOnUiThread
One of the keys to maintaining a responsive GUI is to do long-running tasks on a background thread s ...
- django notes 一:开篇
公司 web 框架用的是 django, 以前没用过,打算这两周好好看看. 边学习边整理一下笔记,加深理解. 好像谁说过初学者更适合写入门级的教程,我觉得有一定道理. 高手写的教程有一定深度,不会写入 ...
- 配置vim插件遇到youcompleteme插件问题解决方案
今天在Opensuse下配置vim 遇到两个问题 配置插件找到一个很好的博客.学到一些有用技巧 http://hahaya.github.io/2013/07/26/use-vundle.html 但 ...
- PL/SQL Developer中输入SQL语句时如何自动提示字段
在PL/SQL Developer中编写sql语句时,如果无法自动提示字段那是一件痛苦的事情,工作效率又低,在此演示下如何在PL/SQL Developer工具中自动提示字段,让开发者省时又省心,操作 ...
- PHP多维数据排序(不区分大小字母)
1. PHP中最普通的数组排序方法 sort(); 看个例子: <?php $test = array(); $test[] = 'ABCD'; $test[] = 'aaaa'; $test[ ...
- Android OpenGL教程-第二课【转】
第二课 你的第一个多边形: 在第一个教程的基础上,我们添加了一个三角形和一个四边形.也许你认为这很简单,但你已经迈出了一大步,要知道任何在OpenGL中绘制的模型都会被分解为这两种简单的图形. 读完了 ...
- cs端调用webApi
public class Httphelper { public static string Post1(string url, string postString) { using (WebClie ...
- Git中.gitignore, 忽略追踪
在目录下 创建: .gitignore文件,将不需要被追踪的文件地址, 写在该文件中, 此时git软件就不会追踪列出的文件进行版本同步: windows不允许创建没有文件名的文件,可以用编辑器创建.g ...