struts2中form表单中的input的类型type=file,提交时在ActionSupport如何得到他的file文件

不用get/set方法的那种,文件名我可以得到,用request.getParameter("pic");图片怎么写啊<formaction="clothes/uploa... 不用get/set方法的那种,文件名我可以得到,用request.getParameter("pic");图片怎么写啊
<form action="clothes/upload_savePicInfo.action" method="post" enctype="multipart/form-data">
form表单加了enctype后就只能有get/set方法才能得到页面上的属性,不能用request.getParameter了,我想用request.getParameter获取属性,所以想去掉
enctype="multipart/form-data",但去掉之后文字属性是得到了,可是文件属性不知该怎么得到?
展开
 我来答
比克搜寻者
2015-09-01 · TA获得超过175个赞
知道小有建树答主
回答量:225
采纳率:0%
帮助的人:42.9万
展开全部
你好按照我摘录的以下编写就可以了
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登陆页面</title>
</head>
<body>
<s:form action="/test/loginAction.action" method="post">
<s:textfield name="userName" label="用户名"/>
<s:password name="password" label="密码"/>
<s:submit/>
</s:form>
</body>
</html>
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登陆页面</title>
</head>
<body>
<s:form action="/test/loginAction.action" method="post">
<s:textfield name="userName" label="用户名"/>
<s:password name="password" label="密码"/>
<s:submit/>
</s:form>
</body>
</html>

请求处理LoginAction.java:

Java代码
package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String msg="显示默认消息";
private String userName;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getMsg() {
return msg;
}

public String execute(){
if ("xiaozhu".equals(userName)&&"111111".equals(password)) {
msg="welcome,"+userName;
} else {
msg="用户名或密码错误";
}
return this.SUCCESS;
}
}
Java代码
package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String msg="显示默认消息";
private String userName;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getMsg() {
return msg;
}

public String execute(){
if ("xiaozhu".equals(userName)&&"111111".equals(password)) {
msg="welcome,"+userName;
} else {
msg="用户名或密码错误";
}
return this.SUCCESS;
}
}

响应页面HelloWorld.jsp:
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="msg"/></h3>
</body>
</html>
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="msg"/></h3>
</body>
</html>

如上所示,对于login.jsp表单中的每个值域我们都会在相应的action中声明一个对应的属性并产生相应的get和set方法,如果这个表单中的值很多(例如注册用户信息的表单),就会导致action十分庞大和容易混乱,并且也不利于我们的软件分层,违背了OO的原则,下面看一下改进后的方案。

登陆页面login.jsp:

Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<s:form action="/test/loginAction.action" method="post">
<s:textfield name="user.userName" label="用户名"/>
<s:password name="user.password" label="密码"/>
<s:submit/>
</s:form>
</body>
</html>
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<s:form action="/test/loginAction.action" method="post">
<s:textfield name="user.userName" label="用户名"/>
<s:password name="user.password" label="密码"/>
<s:submit/>
</s:form>
</body>
</html>

新增实体类User.java:

Java代码
package tutorial;

public class User {
private String userName;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}
Java代码
package tutorial;

public class User {
private String userName;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
}

请求处理LoginAction.java:
Java代码
package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String msg="显示默认消息";
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public String getMsg() {
return msg;
}

public String execute(){
if("xiaozhu".equals(user.getUserName())&&"111111".equals(user.getPassword()))
msg="welcome,"+user.getUserName();
else
msg="用户名或密码错误";
return this.SUCCESS;//
}
}
Java代码
package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String msg="显示默认消息";
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public String getMsg() {
return msg;
}

public String execute(){
if("xiaozhu".equals(user.getUserName())&&"111111".equals(user.getPassword()))
msg="welcome,"+user.getUserName();
else
msg="用户名或密码错误";
return this.SUCCESS;//
}
}

响应页面HelloWorld.jsp:
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="msg"/></h3>
</body>
</html>
Java代码
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello</title>
</head>
<body>
<h3><s:property value="msg"/></h3>
</body>
</htmL>

注意:
Java代码
public User getUser() {
return user;
}
Java代码
public User getUser() {
return user;
}

Action中的这个get方法必须,否则会出现丢值的现象;set方法就更不用说了,必须的。
ihss23
2012-07-09 · TA获得超过701个赞
知道小有建树答主
回答量:1472
采纳率:0%
帮助的人:1335万
展开全部
可以查下
struts2
自带的上传功能。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式