SpringMvc 学习笔记

前言

本篇博客意在尽可能详细的记录学习SpringMvc中所有功能的实现,为自己以后搭建和使用SpringMvc提供方法,同时也给码农们提供一个可以参考和讨论的地方,另外,博主使用的是Eclipse For J2EE的版本,不是使用MyEclipse,所以有些地方使用或者配置不太一样.还有就是博客中重在记录功能的大致实现并不抠细节,所以本篇博客您可以视为是可以作为参考的一篇文章


学习前的SpringTool的安装

访问密码 35c6
图文教程和工具都在里面啦

SpringMvc之HelloWorld

1.加入必须的jar包

commons-logging-1.2.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

2.web.xml配置SpringMvc的请求处理器

<?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">
	<display-name>SpringMvc_HelloWorld</display-name>
	<!-- 处理请求的Servlet -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 应答所有请求 -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

3.编写配置文件spring.xml和spring_mvc.xml

spring.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<!-- 配置自动扫描的包,扫描注解controller,service,rep...... -->
	<context:component-scan base-package="com.xiaojinzi.springMvc"></context:component-scan>

</beans>

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 配置mvc中返回的视图 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

4.编写HelloWorld的请求处理器,由SpringMvc分发

@Controller
@RequestMapping("hello")
public class HelloWorld {

	@RequestMapping("test")
	public String test() {
		System.out.println("请求到来");
		// 表示转发到:/WEB-INF/views/success.jsp 页面
		return "success";
	}

}


5.效果



6.源码下载

https://github.com/xiaojinzi123/springMvc_demo/tree/master/SpringMvc_HelloWorld

SpringMvc之文件上传

1.加入jar包

commons-fileupload-1.3.2.jar
commons-io-2.5.jar
commons-logging-1.2.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
比helloworld多了两个jar

2.配置一个文件上传的bean



这里的name属性的名称是定的,否则不能正常工作


3.编写文件上传的页面

<body>
	<form enctype="multipart/form-data" action="uploadFile/test" method="POST">

		name: <input type="text" name="desc" /> 
		file: <input type="file" name="file" /> 
			<input type="submit" value="提交" />

	</form>
</body>

4.编写请求处理器

@Controller
@RequestMapping("uploadFile")
public class UploadFile {

	@RequestMapping("test")
	public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) {

		System.out.println("desc = " + desc);
		System.out.println("fileName = " + file.getOriginalFilename());
		System.out.println("contentType = " + file.getContentType());

		// 表示转发到:/WEB-INF/views/success.jsp 页面
		return "success";
	}

}


5.运行效果



6.源码下载

https://github.com/xiaojinzi123/springMvc_demo/tree/master/SpringMvc_uploadFile

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值