注解配置中给对应的bean添加org.springframework.context.annotation.Scope注解
//@Scope("singleton") // 容器范围
//@Scope("prototype") //每次获取bean都new
//@Scope("request") //一次http请求一个实例
//@Scope("session") //一个http session中
//@Scope("globalSession")// 一个全局session中有效
//@Scope("application") //application作用域中
//一下作用域需要配置web并在web.xml中配置org.springframework.web.context.request.RequestContextListener监听
不知道上面的监听是否是必须的,不过在spring mvc项目中可以不用加上面的监听也可以设置bean的生命周期
测试,可以直接打印出对应bean的对象地址查看是否一样
/**
*
* Specifies the scope to use for the annotated component/bean.
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
* org.springframework.web.context.WebApplicationContext
*/
package com.score.web.action.picture;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.score.model.members.ScoreMembers;
import com.score.model.picture.ScorePhoto;
import com.score.service.exception.BeanAlreadyExistsException;
import com.score.service.picture.ScorePhotoManager;
import com.score.web.action.core.BaseAction;
import com.score.web.action.upload.UploadFileForm;
public class CreatePhotoAction extends BaseAction{
public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) {
UploadFileForm uploadForm = (UploadFileForm) actionForm;
FormFile file = uploadForm.getFileForm();
if(file.getContentType().equals("image/jpeg")){ //判断类型
System.out.println("jpeg 类型");
}else if(file.getContentType().equals("image/png")){
System.out.println("png 类型");
}else if(file.getContentType().equals("image/gif")){
System.out.println("gif 类型");
}else if(file.getContentType().equals("image/pjpeg")){
System.out.println("jpg 类型");
}else{
System.out.println("不是图片!");
request.setAttribute("errorFlag", "1");
return mapping.findForward("listpicture");
}
if(file.getFileSize()>2097152){ //判断大小
System.out.println("大于2M");
request.getSession().setAttribute("errorFlag",2);
return mapping.findForward("listpicture");
}
String uri = membersHead(request,file);
request.setAttribute("uri", uri);
ScorePhotoManager spm=(ScorePhotoManager)getBean("scorePhotoManager");
ScorePhoto sp=new ScorePhoto();
sp.setPhotoAddr(uri);
try {
spm.createScorePhoto(sp);
} catch (BeanAlreadyExistsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getSession().setAttribute("errorFlag",1);
return mapping.findForward("listpicture");
}
//membersHead方法可以写到service层,然后调用,此处为方便理解,写在action中
//membersHead方法判断文件的大小和类型
// 会员资料文件
public String membersHead(HttpServletRequest request,FormFile file) {
String uri = "";
String dir = servlet.getServletContext().getRealPath("/upload");
// 文件夹
File dirFile = null;
boolean bFile = false;
dirFile = new File(dir);
bFile = dirFile.exists();// true 该文件夹存在,false 不存在
if (bFile == false) {
bFile = dirFile.mkdirs();
if (bFile == false) {
System.out.println(" 文件夹创建失败!");
}
}
if (bFile == true) {
try {
InputStream input = file.getInputStream();// 能从FormFile中获得输入流
String uid = UUID.randomUUID().toString();
String fileName = file.getFileName();
fileName = uid + fileName.substring(fileName.lastIndexOf("."));
OutputStream output = new FileOutputStream(dir + "/" + fileName);
int bytesReader = 0;
byte[] readbuffer = new byte[102400];
while ((bytesReader = input.read(readbuffer, 0, 102400)) != -1) {
output.write(readbuffer, 0, bytesReader);
}
output.close();
String protco = request.getProtocol().substring(0,
request.getProtocol().indexOf("/"));
String webPath = protco + "://" + request.getServerName() + ":"
+ request.getServerPort();
// 资料路径
uri = webPath + "/upload/"+ fileName;
} catch (Exception e) {
e.printStackTrace();
}
}
return uri;
}
}
附:
/**
* 所要上传的文件的Form
* @author UploadFileForm
* @version $Revision: $ $Date: $
*/
public class UploadFileForm extends BaseForm implements java.io.Serializable{
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
在自己实现的FilterInvocationSecurityMetadataSource接口的子类中的
getAttributes(Object arg0)方法里
javax.servlet.http.HttpServletRequest
request = ((org.springframework.security.web.FilterInvocation
)arg0).getRequest();
可以获得request对象
org.springframework.security.core.Authentication
auth = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
但是这里只是获取了用户的简单信息,获取更多信息应该得自己组织
-------------------------没有试过,别人说的
jsp页面中用security标签可以获得用户名
<sec:authentication property="name"></sec:authentication>
request对象可以获得用户信息
request.getUserPrincipal().getName();