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",但去掉之后文字属性是得到了,可是文件属性不知该怎么得到? 展开
<form action="clothes/upload_savePicInfo.action" method="post" enctype="multipart/form-data">
form表单加了enctype后就只能有get/set方法才能得到页面上的属性,不能用request.getParameter了,我想用request.getParameter获取属性,所以想去掉
enctype="multipart/form-data",但去掉之后文字属性是得到了,可是文件属性不知该怎么得到? 展开
2个回答
展开全部
你好按照我摘录的以下编写就可以了
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方法就更不用说了,必须的。
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方法就更不用说了,必须的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询