在struts2中没有为文件输入调用Set方法,而在其他性能参数中调用了Set方法

问题描述 投票:0回答:1

我是struts2的新手。我正在尝试从客户端获取数据并将其插入db。但是在执行此setImageFile(File fie)时不会被调用,因此imageFile对象为null。虽然其他set方法被调用,因此各个成员包含值。

action.class

    import com.opensymphony.xwork2.ActionSupport;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Map;
    import javax.servlet.http.Part;
    import org.apache.struts2.interceptor.SessionAware;
    import org.apache.struts2.dispatcher.SessionMap;
    public class register extends ActionSupport implements SessionAware{
        SessionMap<String, String> sessionMap;
        private String name;
        private String email;
        private String dob;
        private String address;
        private File imageFile;
        private String imageFileFileName;
        private String imageFileContentType;

        @Override
        public void setSession(Map map)
        {
         sessionMap = (SessionMap)map;
         sessionMap.put("", name);
        }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getDob() {
            return dob;
        }

        public void setDob(String dob) {
            this.dob = dob;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
        public File getImageFile() {
            return imageFile;
        }


      public void setFile(File imageFile) {
            this.imageFile = imageFile;
       }
     public void setSessionMap(SessionMap<String, String> sessionMap) {
        this.sessionMap = sessionMap;
    }








      public String execute()
       {
           try
           {
           Class.forName("org.postgresql.Driver");

           Connection conn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/DemoDB", "postgres", "postgres");
           InputStream imageInputStream = new FileInputStream(imageFile);
           String query = "insert into userProfile('Name','Address','email','dob','img') values(?,?,?,?,?)";
           Date date = Date.valueOf(dob); 
           PreparedStatement preparedStatement =   conn.prepareStatement(query);
           preparedStatement.setString(0, name);
           preparedStatement.setString(1, address);
           preparedStatement.setString(2, email);
           preparedStatement.setDate(3, date);
           preparedStatement.setBinaryStream(4, imageInputStream, imageFile.length());
           preparedStatement.execute();
           }
           catch(Exception e)
           {
                 System.out.println(e.getMessage());
           }
           return "success";
       }
    }

在上面的代码中imageFile为null。其他成员包含价值

struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts   
    Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
    <struts>
        <package name="register" namespace="/" extends="tiles-default">
            <action name="register" class="com.passportseva.register">

                <result name="success" type="tiles">login</result>
                <result name="input" type="tiles">register</result>
                <result name="error">layoutmanager.jsp</result>
            </action>
        </package>
    </struts>

这是在其中调用动作的jsp

registration.jsp

        <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <form action="register" method="POST">
                <div>
                    <div><h1>Name</h1></div>
                    <input type="text" name="name" hint="name"/>
                </div>
                <div>
                    <div><h1>Email</h1>></div>
                    <input type="text" name="email" hint="email"/>
                </div>
                <div>
                    <div><h1>DOB</h1></div>
                    <input type="date" name="dob"/>
                </div>
                <div>
                    <div><h1>Address</h1></div>
                    <input type="area" name="address" hint="address"/>
                </div>
                <div>
                    <s:file name="imageFile" label="image"/>
                </div>
                <input type="submit" value="register"/>
            </form>
        </body>
    </html>

我在这里用瓷砖Tiles.xml

    <!DOCTYPE tiles-definitions PUBLIC   
    "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"  
     "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> 
     <tiles-definitions>
         <definition name="login" template="/layoutmanager.jsp">
             <put-attribute name="title" value="Login Page"/>
             <put-attribute name="body" value="/login.jsp"/>
         </definition>
          <definition name="register" template="/layoutmanager.jsp">
             <put-attribute name="title" value="Passport seva registration"/>
             <put-attribute name="body" value="/registration.jsp"/>
         </definition>
     </tiles-definitions>

输出显示imageFile的无效字段值。

struts2 struts2-interceptors
1个回答
0
投票

将Struts Form标记与enctype属性一起使用。

<s:form action="register" method="POST" enctype="multipart/form-data">
</s:form>
© www.soinside.com 2019 - 2024. All rights reserved.