Java 将会话参数传递给另一个控制器并在springboot中验证它们

Java 将会话参数传递给另一个控制器并在springboot中验证它们,java,file,session,spring-boot,modelattribute,Java,File,Session,Spring Boot,Modelattribute,我正在尝试从springboot中的文件实现文本搜索。我已成功地将文件上载到目录。但是,当我试图将searchword和文件名作为会话属性传递给下一个控制器时。我无法实现文本搜索。我需要知道如何验证会话参数中传递的值是否正确,以及如何将其映射到结果页的GET请求处理程序中的model属性 这是我获取上传文件并初始化会话参数的控制器。 FileUploadController.java @Controller @SessionAttributes({"searchText","fileName"}

我正在尝试从springboot中的文件实现文本搜索。我已成功地将文件上载到目录。但是,当我试图将searchword和文件名作为会话属性传递给下一个控制器时。我无法实现文本搜索。我需要知道如何验证会话参数中传递的值是否正确,以及如何将其映射到结果页的GET请求处理程序中的model属性

这是我获取上传文件并初始化会话参数的控制器。 FileUploadController.java

@Controller
@SessionAttributes({"searchText","fileName"})
public class FileUploadController {

    @Autowired
    StorageService storageService;

    Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @RequestMapping(value="/",method=RequestMethod.GET)
    public String listUploadedFiles(ModelMap model) {
        return "uploadForm";
    }

    @RequestMapping(value="/",method=RequestMethod.POST)
    public String handleFileUpload(@RequestParam(required=true) String 
    searchText,@RequestParam("file") MultipartFile file,ModelMap model) {
        try{
            storageService.store(file);
            if(!model.containsAttribute("searchText"))
                model.addAttribute("searchText", searchText);

            if(!model.containsAttribute("fileName"))
                model.addAttribute("fileName", file.getOriginalFilename());
        }
        catch(Exception e) {
            model.addAttribute("message", "FAIL to upload " + 
            file.getOriginalFilename() + "!");
            return "uploadForm";
        }
        return "resultData";
    }
}
这是结果页面的控制器

@Controller
@RequestMapping(value="/resultData")
@SessionAttributes({"searchText","fileName"})
public class ResultController {
    @Autowired
    StorageService storageService;

    @Autowired
    WordSearchService service;

    Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @RequestMapping(method = RequestMethod.GET)
    public String processResult(@ModelAttribute("searchText") String 
                                searchText,@ModelAttribute("fileName") 
                                String fileName,ModelMap model){
        List<String> searchTexts = new ArrayList<String>();
        System.out.println("Getting results page");
        model.addAttribute("searchText", searchText);
        searchTexts = service.getList(searchText);
        System.out.println(searchText);

        model.addAttribute("fileName", fileName);

        List<String> list = new ArrayList<String>();


        /*Reads the file based on the list of query words and results are 
          stored in the hashmap<String,String> lineSearch for storing the 
          lines containg the words and hashmap<String,Integer> word for 
          storing each query word with the number of times it appears in the 
          file */

        try(Stream<String> stream = 
        Files.lines(storageService.loadFile(fileName))){
            for(String temp : searchTexts)
                list = stream.filter(line->
                line.contains(temp)).collect(Collectors.toList());

             int total = 0;
             Iterator<String> itr = list.iterator();
             for(String temp: searchTexts){
                while(itr.hasNext()){
                    String line = itr.next();
                    if(line.contains(temp)){
                        total = service.countWord(line, searchText);
                        service.insertWordCount(searchText,total);
                        service.insertLineSearch(line, searchText);

                        if(!model.containsAttribute("lineSearch")){
                            model.addAttribute("lineSearch", 
                            service.getLineSearch());
                        }
                        if(!itr.hasNext()){
                            if(!model.containsAttribute("word")){
                                model.addAttribute("word", 
                                service.getWordCount());
                            }
                            total = 0;
                        }
                    }   
                }
                stream.close();
            }
        }catch(IOException e){
            log.error(e.getMessage());
        }
        return "resultData";
    }
}
@控制器
@请求映射(value=“/resultData”)
@SessionAttributes({“searchText”,“fileName”})
公共类结果控制器{
@自动连线
存储服务存储服务;
@自动连线
文字搜索服务;
Logger log=LoggerFactory.getLogger(this.getClass().getName());
@RequestMapping(method=RequestMethod.GET)
公共字符串processResult(@ModelAttribute(“searchText”)字符串
searchText,@ModelAttribute(“文件名”)
字符串文件名,ModelMap模型){
List searchtext=new ArrayList();
System.out.println(“获取结果页”);
model.addAttribute(“searchText”,searchText);
searchText=service.getList(searchText);
System.out.println(searchText);
model.addAttribute(“文件名”,文件名);
列表=新的ArrayList();
/*根据查询词列表读取文件,结果为
存储在hashmap lineSearch中,用于存储
包含单词和单词hashmap的行
存储每个查询词及其在
文件*/
try(流=
Files.lines(storageService.loadFile(文件名))){
用于(字符串临时:搜索文本)
列表=stream.filter(行->
line.contains(temp)).collect(Collectors.toList());
int-total=0;
迭代器itr=list.Iterator();
用于(字符串临时:搜索文本){
while(itr.hasNext()){
字符串行=itr.next();
if(行包含(温度)){
总计=service.countWord(行,搜索文本);
insertWordCount(searchText,总计);
insertLineSearch(行,searchText);
如果(!model.containsAttribute(“lineSearch”)){
model.addAttribute(“lineSearch”,
service.getLineSearch());
}
如果(!itr.hasNext()){
如果(!model.containsAttribute(“word”)){
model.addAttribute(“word”,
service.getWordCount());
}
总数=0;
}
}   
}
stream.close();
}
}捕获(IOE异常){
log.error(例如getMessage());
}
返回“resultData”;
}
}
这是上传页面uploadForm.html

 <html xmlns:th="http://www.thymeleaf.org">
    <body>

        <h2>Please upload the file to search for the word </h2>

        <div>
            <form method="POST" enctype="multipart/form-data" action="/">
                <table>
                    <tr><td>Enter Search Text:</td>
                        <td><input type="text" name="searchText"></input></td>           
                    </tr>
                    <tr><td>File to upload:</td>
                        <td><input type="file" name="file" /></td></tr>
                    <tr><td><input type="submit" value="Upload" /></td></tr>
                </table>
            </form>
        </div>

    </body>
    </html>

请上传文件以搜索单词
输入搜索文本:
要上载的文件:
这是结果页面resultData.html

<html xmlns:th="http://www.thymeleaf.org">
            <body>
            <h2>List All Uploaded Files</h2>
            <div>
                 <p><b>Searching for the query ${searchText} in the file 
                      ${fileName}</b>
                     The expected results from the API call is:</p>

                 <div th:each="searchWord : ${word}">
                 <table>
                 <tr>
                 <td th:text="${searchWord.key}"> - found </td> 
                 <td th:text="${searchWord.value}"> time(s)</td>
                 </tr>
                 <tr th:each="Line:${lineSearch}">
                 <td th:text="${line.value}" 
                    th:unless="${line.value}!=${word.key}"></td>
                 </tr>
                 </table>
                 </div>
            </div>    
        </body>
    </html>

列出所有上传的文件
在文件中搜索查询${searchText}
${fileName}
API调用的预期结果是:

-发现 时间(s)
在SpringBoot中,似乎很难将会话参数的值传递并分配给第二个控制器方法中的另一个参数或模型属性。我在springboot中使用@SessionAttributes获得了以下视频的帮助,但到目前为止我还没有成功

以下是链接: