三、struts分派action.动态表单
(一)DispatchAction-分派action
(二)Dynamic Form-动态表单
(三)struts中的文件上传下载
(四)内部留言本小项目
(五)Mvc中如何上传文件
DispatchAction:分派Action
为什么需要dispatchAction?
案例说明
步骤:
1.创建web工程
2.引入struts
3.配置action
<!-- parameter用于将来区分用户不同的请求 -->
<action-mappings >
<action
attribute="userForm"
input="/WEB-INF/ok.jsp"
name="userForm"
parameter="flag"
path="/loginAndLoginOut"
scope="request"
type="com.hyf.web.struts.action.LoginAndLoginOutAction"
validate="false"
cancellable="true">
<forward name="loginok" path="/WEB-INF/ok.jsp" />
<forward name="goLogin" path="/WEB-INF/login.jsp" />
</action>
</action-mappings>
4.看图说明jsp/action/struts-config三者的关系
全局跳转
<global-forwards >
<forward name="goerr" path="/WEB-INF/err.jsp" />
</global-forwards>
所谓全局跳转,指的是所有的action都可以跳转该页面
所谓局部跳转,指的是只有本action可以跳转到该页面
注意:千万不要和全局跳转连线,直接
return mapping.findForward("goerr");
即可
Dynamic Form
使用动态表单可以更加灵活的使用表单,在某些不能确定表单个数和类型的情况下,可以使用动态表单(Dynamic Form)来解决问题
为什么需要Dynamic Form?
当我们表单类的属性个数和类型不能确定的时候,我们可以使用动态表单来解决
动态表单和普通表单类区别在
1.普通表单类ActionForm是首先需要定义这样一个类,侯然配置到struts-config.xml中
2.动态表单完全依赖反射机制创建,所以不需要定义表单类,直接在struts-config.xml中配置即可
案例说明
如何在Struts-config.xml配置动态表单
<form-beans>
<form-bean name="userForm" type="org.apache.struts.action.DynaActionForm">
<!-- 该表单的属性是配置出来 -->
<form-property name="name" type="java.lang.String"></form-property>
<form-property name="password" type="java.lang.String"></form-property>
<form-property name="email" type="java.lang.String"></form-property>
</form-bean>
</form-beans>
如何在对应的action中取出数据
public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
DynaActionForm userForm = (DynaActionForm) form;// TODO Auto-generated method stub
//从动态表单中取出数据
System.out.println("111111111111");
String name=userForm.getString("name").toString();
String password=userForm.getString("password").toString();
String email=userForm.getString("email").toString();
System.out.println(name+" "+password+" "+email);
return mapping.findForward("registerok");
}
使用struts框架完成文件上传下载
原理
问题:
1.一个Action是否必须要配置一个ActionForm?可以没有
2.一个Action是否可以配置多个表单?最多一个
步骤:
1.建立web工程
2.引入struts
3. Register.jsp
<body>
<h1>注册用户</h1>
<!-- 如果我们的表单有文件控件,则需要重新制定表单的编码方式 -->
<form enctype="multipart/form-data" action="/strutsFileupAndDown/register.do" method="post">
名字:<input type="text" name="name"><br>
头像:<input type="file" name="myphoto"><br>
<input type="submit" value="注册用户">
</form>
</body>
4.写 UserForm表单和RegisterAction
5.牵线
6.写业务逻辑
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
String user=userForm.getName();
FormFile formFile=userForm.getMyphoto();
//通过formFile我们可以湖区关于用户上传文件的各种信息
String filename=formFile.getFileName();
int filesize=formFile.getFileSize();
System.out.println(filename+" "+filesize);
//获取输入流
InputStream is=null;
OutputStream os=null;
try {
is= formFile.getInputStream();
//得到一个输出流->文件
//1.得到file文件夹上传到奥tomcat服务器后绝对路径
String keepFilePath= this.getServlet().getServletContext().getRealPath("/file");
System.out.println(keepFilePath+"\\"+filename);
os=new FileOutputStream(keepFilePath+"\\"+filename);
//读取文件并写出到服务器file
int len=0;
//做一个buffer
byte[] bytes=new byte[1024];
//循环处理
while((len=is.read(bytes))>0){
//读一点,写出一点
os.write(bytes,0,len);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
问题1:中文名字的文件?2:存在文件会覆盖
解决文件上传的细节问题
1.覆盖文件的问题
思路:
编写一个工具类,可以生成不重复的文件名,然后保存
public static String getNewFileName(String fileName){
int beginIndex=fileName.lastIndexOf(".");
return UUID.randomUUID().toString()+fileName.substring(beginIndex, fileName.length());
在action调用即可
2.如果文件名出现中文怎么办
2.1首先使用过滤器
2.2待会下载的时候,如何给用户提供原来真实的文件名
考虑文件下载?
因为我们下载的时候提示上传文件的名称和人名,因此我们需要把信息保存在数据库
users表
create table users(
Username varchar(64) unique not null,
photos varchar(128) not null,--保存新的文件名
photos2 varchar(128) not null--保存真实的文件名
)