简体   繁体   English

通过GSP和Groovy代码以表格或textArea,textForm形式显示文本值

[英]Display text value in a form or a textArea, textForm by GSP and Groovy code

So i have an uploadForm and after i choosed file to upload,and i submit,and therefore i want to display content of file documents to an textArea or Form,but i don't know how to get parameter to that file and render text in that file. 所以我有一个uploadForm,在我选择要上传的文件并提交之后,因此我想将文件文档的内容显示到textArea或Form上,但是我不知道如何获取该文件的参数并在其中渲染文本该文件。
Here is my code:file displayForm.gsp 这是我的代码:文件displayForm.gsp

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Upload</title>
</head>
<body>
<div class="body">
</div>
<g:uploadForm action ="uploaded" >
    <input type = "file" name = "cfile" value = "File"/>
    <g:actionSubmit  action="displayForm" value = "submit" />
</g:uploadForm>
    <p>
        <textField rows="50" cols="100" value = "${cfile}"
        name="displayResult"/ >          
    </p>
    <input type="button" value = "Display" name = "Display">
    <br />
</body>
</html>  

And this is file SampleController.groovy : 这是文件SampleController.groovy

class SampleController{
def f
def displayForm() {
    if(params.cfile){
        if(params.cfile instanceof        org.springframework.web.multipart.commons.CommonsMultipartFile){                  //new FileOutputStream('D:/submitted_file.txt').leftShift( params.cfile.getInputStream() );
            f=params.cfile.transferTo(new File('D:/submitted_file.uima'));
            render file:params.cfile.toString() ,contentType: "text", encoding:"UTF-8"  
        }else{
            log.error("wrong attachment type [${cfile.getClass()}]");
        }
    }
}

As you can see above,i wan't to read file 'D:/submitted_file.uima' and render it. 如您在上面看到的,我不会读取文件'D:/submitted_file.uima'并进行渲染。
but if i call render method,it got error that file is null. 但是,如果我调用render方法,则会收到文件为null的错误消息。

I got a problem at call file parameter from file .gsp.Should i call name "cfile" or "value" or "id" or another from uploadForm and textArea(textField). 我从文件.gsp调用文件参数时遇到问题。我应该从uploadForm和textArea(textField)调用名称“ cfile”还是“ value”或“ id”,还是另一个。

The render file:x method accepts byte[] , java.io.File or inputStream parameters while you seem to be passing in a CommonsMultipartFile . 当您似乎要传递CommonsMultipartFilerender file:x方法接受byte[]java.io.FileinputStream参数。 Try calling render file: f, ... in the above code, instead, to actually render the file you saved to disk. 尝试调用上述代码中的render file: f, ... ,以实际渲染您保存到磁盘的文件。

You have to create this file, if one does not exist. 如果不存在,则必须创建此文件。

File file = new File("C:/test.txt");
if(!file.exists()) {
   file.createNewFile();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM