eclipse springmvc配置

springmvc的一些配置使用

web.xml中配置的加载优先级context-param -> listener -> filter -> servlet。

①context-param加载

context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息所以context-param最先加载。

②Listener加载

  • Spring提供ServletContentListener的一个实现类ContextLoaderListener监听器,该类可以作为Listener使用,在启动Tomcat容器的时候,该类的作用就是自动装载ApplicationContext的配置信息,如果没有设置contextConfigLocation的初始参数则会使用默认参数WEB-INF路径下的application.xml文件。如果需要自定义读取多个配置文件或者修改默认路径,则可以在web.xml中设置:
<listener>		
	<listenerclass>org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>

自定义设置:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/applicationContext.xml,
		</param-value>
	</context-param>

③ filter加载

<!-- UTF-8编码 -->
	<filter>
		<filter-name>encodingConvertFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

④servlet加载

<!-- DispatcherServlet核心控制器, mvc每一个请求都要经过这个,访问虚拟地址 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:/applicationContext-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

⑤applicationContext.xml配置:

<!-- spring父容器加载其他组件如service,dao,不加载controller -->
	<context:component-scan base-package="com">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

⑥applicationContext.xml配置:

<!-- springmvc子容器加载注解,扫描指定路径下的controller文件 -->
<context:component-scan base-package="com"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven> </mvc:annotation-driven>

⑦springmvc配置视图解析器

<!--JSP视图解析器 加上前后缀 只对转发forward有效,对重定向redirect无效 -->
	<bean id="viewResolverJsp"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

⑧上传文件配置

1、 applicationContext-mvc.xml配置

<!-- 上传文件配置    id 为 multipartResolver, 不要commons-->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 配置文上传编码 -->
		<property name="defaultEncoding" value="UTF-8" />
		<!-- <property name="maxUploadSize" value="1048576"></property> -->
	</bean>

2、 java controller代码

单文件上传:

public void singleFileUpload(MultipartFile file, HttpServletRequest request) throws Exception {
		// 1、getOriginalFilename:获取上传文件的文件名
		String originFilename = file.getOriginalFilename();
		// 2、将上传的文件保存到指定的目录下
		// 获取 uuid
		String uuid = UUID.randomUUID().toString();
		String path = request.getServletContext().getRealPath("/upload");
		File filePath = new File(path + "\\" + uuid + originFilename);
		// 将文件保存到tomcat的upload文件夹。
		file.transferTo(filePath);
}

多文件上传:

	public Map<String, Object> singleFileUpload(MultipartRequest files, HttpServletRequest request) throws Exception {
		Map<String, Object> filePathMap = new Map<String, Object>();
		java.util.Map<String, MultipartFile> filesMap = files.getFileMap();
		Set<String> keySet = filesMap.keySet();
		for(String key : keySet) {
			MultipartFile file = filesMap.get(key);
			// 1、getOriginalFilename:获取上传文件的文件名
			String originFilename = file.getOriginalFilename();
			// 2、将上传的文件保存到指定的目录下
			// 获取 uuid
			String uuid = UUID.randomUUID().toString();
			String path = request.getServletContext().getRealPath("/upload");
			String positivePath = uuid + originFilename; //相对路径
			File filePath = new File(path + "\\" + uuid + originFilename);
			// 将文件保存到tomcat的upload文件夹。
			file.transferTo(filePath);
			// 开始将路径保存到map集合中去,便于存到数据库
			filePathMap.put(uuid,positivePath);
		}
		
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值