十四、MVC的WEB框架(Structs2)

一、Structs2中的Session

1、一个是传统的servlet包下的HttpSession,一个是Structs2中自己定义的Session

Servlet下的Session获取方法:ServletActionContext.getRequest().getSession(),需要servlet-api.jar包

2、Structs中的Session获取方法:Map m=ActionContext.getContext().getSession(),session以Map类的形式,其中值和HttpSession中的值是同步的。

package com.demo.action;

import java.util.Map;

import com.demo.Model.Product;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class ProductAction extends ActionSupport {
private Product product; public String show() {
product = new Product();
product.setName("iphone7");
return "show";
} public String add() {
//通过session把name放进去
Map m = ActionContext.getContext().getSession();
m.put("name", product.getName());
return "show";
} public Product getProduct() {
return product;
} public void setProduct(Product product) {
this.product = product;
}
}

3、在show.jsp中通过EL表达式把session中的数据取出来

<%@page isELIgnored="false"%>
 
${product.name}
 
${name}<!--EL表达式可以直接从session中数据取出来-->

二、文件上传

1、准备upload.jsp

上传文件一定要为form表单加上enctype="multipart/form-data",表示提交的数据是二进制的,并且必须是method="post"

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%> <%@page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %> <html>
<body> <form action="upload" method="post" enctype="multipart/form-data"> 上传文件 : <input type="file" name="doc" /> <br> <!--使用structs的s:file标签表示上传文件框,name使用doc会和服务端的相应的代码匹配-->
上传文件2:<s:file name="doc" label="upload"/>
<input type="submit" value="上传">
</form> </body>
</html>

2、准备UploadAction

由于在upload.jsp中file字段对应的name是doc,在Action中,必须准备三个属性:File doc;   String docFileName;  String docContentType;属性名字不能使用其他的,只能基于doc

package com.demo.action;

import java.io.File;
import java.io.IOException;
import java.util.UUID; import org.apache.commons.io.FileUtils;
import org.apache.structs2.ServletActionContext; public class UploadAction{
File doc;
String docFileName;
String docContentType; //属性的getter/setter方法
public File getDoc(){
return doc;
}
public void setDoc(File doc){
this.doc=doc;
}
public String getDocFileName(){
return docFileName;
}
public void setDocFileName(String docFileName){
this.docFileName=docFileName;
}
public String getDocContentType(){
return docContentType;
}
public void setDocContentType(String docContentType){
this.docContentType=docContentType;
} public String upload(){
System.out.println(doc);
System.out.println(docFileName);
System.out.println(docContentType);
return "success";
}
}

3、配置structs.xml

为upload路径配置UploadAction,并返回success.jsp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="basicstruts" extends="struts-default">
<action name="showProduct" class="com.demo.action.ProductAction" method="show">
<result name="show">show.jsp</result>
</action>
<action name="addProduct" class="com.demo.action.ProductAction" method="add">
<result name="input">addProduct.jsp</result>
<result name="show">show.jsp</result>
</action>
<!--上传文件-->
<action name="upload" class="com.demo.action.UploadAction" method="upload">
<result name="success">success.jsp</result>
</action>
</package>

4、准备success.jsp

显示doc,docFileName,docContentType

<%@page isElIgnored="false"%>
uploaded success
${doc}
<br/>
${docFileName}
<br/>
${docContentType}
上一篇:[SQL Server]利用索引改善sql语句


下一篇:c# – 区域激活和区域RequestNavigate有什么区别?