刷新后如何在文本字段中保留值?

时间:2013-03-11 05:11:14

标签: java jsp

我正在上传表单中的文件,该表单还包含一些textfields我在文本字段中输入了一些值。我想在单击上传按钮时保留此值。并且还有一个保存按钮,当我单击此按钮时,上传的文件应保存在数据库中。谁能帮我吗?

JSP文件在这里:

<body>
    <form action="./upload" enctype="multipart/form-data" >ID: <input type="text" name="id" value="" />Name: <input type="text" name="name" value="" />
       <input name="uploaded" type="file" />
       <input name="save" type="submit" value="Upload" />

       <input type="submit" value="save1" name="save" /></form>
</body>

我需要servlet中的业务逻辑..

3 个答案:

答案 0 :(得分:0)

您的选择是:

最简单的选择是IFRAME。

答案 1 :(得分:0)

嘿,你说的是点击上传按钮时文本字段值应该在那里。你不必做这件事。默认情况下它会在那里。你应该让人流血吗?请提及您的确切要求。

答案 2 :(得分:0)

请参阅没有规定使用value属性保留文件字段数据。

See this link

package comunity.stackoverflow.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestController
 */
public class TestController extends HttpServlet {
    private static final long serialVersionUID = 1L;
public TestController() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    process(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    process(request, response);
}

private void process(HttpServletRequest request,
        HttpServletResponse response) {
    storeInRequest(request, "id");
    storeInRequest(request, "name");
    storeInRequest(request, "uploaded");

    // write your upload logic here then navigate to the same page;

    try {
        request.getRequestDispatcher("/test.jsp").forward(request, response);
    } catch (ServletException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void storeInRequest(HttpServletRequest request,String param){
    String val =  request.getParameter(param);
    if(param!=null && !param.isEmpty()){
        System.out.println(val);
        request.setAttribute(param, val);
    }
}

}

使用standard.jat和jstl.jar

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="upload.do" enctype="multipart/form-data" >
    ID: <input type="text" name="id" value="${id}"/>
    Name: <input type="text" name="name" value="${name}" />
       <input name="uploaded" type="file" />
       <input name="save" type="submit" value="Upload" />

       <input type="submit" value="save1" name="save" /></form>

</body>
</html>

使用此JSP文件。它可能会解决您的问题。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="upload.do" enctype="multipart/form-data" >
    ID: <input type="text" name="id" value="${id}"/><br/>
    Name: <input type="text" name="name" value="${name}" /><br/>
    <input type="file" id="selectedFile" style="display: none;" />
    <input type="text" name="uploaded" id="uploaded" value="${uploaded}" readonly="readonly" size="60">
    <input type="button" value="Browse..." onclick="mymethod()" /><br/>
       <input name="save" type="submit" value="Upload" />

       <input type="submit" value="save1" name="save" /></form>

</body>
<script type="text/javascript">
function mymethod(){
    document.getElementById('selectedFile').click();
    var val = document.getElementById('selectedFile').value;
    document.getElementById('uploaded').value = val;
}
</script>
</html>