介绍(S & T)
“物业报修系统”项目里有个图片上传的功能模块,我花了近一个晚上理解原理和写出代码。项目框架是SSH2,而上传图片模块主要涉及到Struts2,所以我剥离Spring和Hibernate的内容,只放出Struts2的代码。
(N : 在2015年的时候,我在Android端写过一个类似的模块,但当时我的做法是调用
Restful API将图片直接上传到服务器,写的跑在客户端上,而现在写的代码跑在服务器上,两者有较大的区别)
详细步骤(A)
主要步骤分别有Struts2配置文件的修改、编写JSP代码、编写后台Action处理。
Struts2配置文件的修改
首先是web.xml,它是TomCat服务器的配置文件,为了使得网站的默认页面是上传照片页面(default_upload_pic.jsp),必须修改它:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 所有请求都通过FilterDispatcher查找actionMapper的设置来决定请求对应的是哪个Action -->
<display-name>邱永臣配置</display-name>
<!-- 过滤器的配置 -->
<filter>
<!-- 名字 -->
<filter-name>filterDispatcher</filter-name>
<!-- 过滤器对应的名字 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>filterDispatcher</filter-name>
<!-- 过滤匹配的URL -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 解决中文乱码问题 -->
<filter>
<filter-name>struts-clean</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-clean</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 进入WEB应用访问的默认文件 -->
<welcome-file-list>
<welcome-file>default_upload_pic.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 如果将该xml文件放在WEB-INF下面的话,就需要下面配置 <param-value>WEB-INF/applicationContext.xml</param-value> -->
<param-value>WEB-INF/applicationContext.xml</param-value>
<!-- 下面是默认配置 -->
<!-- <param-value>classpath:com/lzq/config/applicationContext-*.xml</param-value> -->
</context-param>
<!-- 采用Listener来初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</list

&spm=1001.2101.3001.5002&articleId=51399121&d=1&t=3&u=7c7955b867724899946d2bbb4fe6ac9e)
1905

被折叠的 条评论
为什么被折叠?



