【问题标题】:error 405 when trying to pass value to Controller from View using spring mvc?尝试使用spring mvc将值从视图传递给控制器​​时出现错误405?
【发布时间】:2018-03-16 11:50:20
【问题描述】:

所以我通过此代码的目的是:通过单击解析按钮,我希望控制器 OssLogParsserenter 检索写入在我的区域文本中名为“chemin”的文件的路径,以便我可以访问该文件并解析它所以我想做什么,但我得到一个 405 错误我的代码有什么问题请帮助我?

Ps:我不想使用<input type="file">

这里是我的日志导入 jsp 文件:

<%@ include file="/WEB-INF/views/includes/includes.jsp" %>
<!DOCTYPE html>
<html lang="fr">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>OSS PROBLEMS - Predict and Solve</title>

    <!-- Bootstrap Core CSS -->
    <link href="<%=request.getContextPath() %>/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- MetisMenu CSS -->
    <link href="<%=request.getContextPath() %>/resources/vendor/metisMenu/metisMenu.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="<%=request.getContextPath() %>/resources/dist/css/sb-admin-2.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="<%=request.getContextPath() %>/resources/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
            
            <%@ include file="/WEB-INF/views/menu_top/topMenu.jsp" %>
			
            <%@ include file="/WEB-INF/views/menu_left/leftMenu.jsp" %>
            <!-- /.navbar-static-side -->
        </nav>

        <!-- Page Content -->
        <div id="page-wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                        <div class="panel-heading">
                            CMExport
                        </div>
                        <div class="panel-body">
                        <c:url value="/logimport/parse" var ="urlparse" />
								
                            <div class="row">
                                <div class="col-lg-6">
                                    <form method="post" role="form">
                                        
                                        <div class="form-group">
                                            <label>CMExport</label>
                                            <input class="form-control" placeholder="Enter chemin du log"  name="chemin">
  
                                        </div>
                                        
                                  
                                        <ol class="breadcrumb">
						  <li><a href="<c:url value="/logimport/parse" />" ><fmt:message code="common.parse" /></a></li>
						  
						</ol>
                                        
                                        <div class="form-group">
                                            <label>log message</label>
                                            <textarea class="form-control" rows="3"></textarea>
                                        </div>
                                       
                                       
                                        
                                       
                                    </form>
                                </div>
                               
                                <!-- /.col-lg-6 (nested) -->
                            </div>
                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                   
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- jQuery -->
    <script src="<%=request.getContextPath() %>/resources/vendor/jquery/jquery.min.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="<%=request.getContextPath() %>/resources/vendor/bootstrap/js/bootstrap.min.js"></script>

    <!-- Metis Menu Plugin JavaScript -->
    <script src="<%=request.getContextPath() %>/resources/vendor/metisMenu/metisMenu.min.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="<%=request.getContextPath() %>/resources/dist/js/sb-admin-2.js"></script>

</body>

</html>

这是我的 OssLogParser 控制器:

package com.oss.controllers;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ericsson.oss.entites.Dmr_dmtool;
import com.ericsson.oss.services.IDmr_dmtoolService;



@Controller
@RequestMapping (value="/logimport")

public class OssLogParserController { 
String path ;

@Autowired 
private IDmr_dmtoolService Dmr_dmtoolService ;

@RequestMapping (value="/")

public String logimport() {
    return "logimport/logimport";

    }
@RequestMapping(value="/parse",method=RequestMethod.POST)

 public  String OssLogParser(HttpServletRequest request ) throws 
 ParseException   {
 path =request.getParameter("chemin");

             BufferedReader br = null;

                try {

                    String line;

                    br = new BufferedReader(new FileReader(path));
                     String nameOfLog =br.readLine().toString();

                        if (nameOfLog.indexOf("/dmr/dmtool")!= -1){
                            line = br.readLine();
                            line = br.readLine();

                            Date date = new Date() ;
                            DateFormat df = new SimpleDateFormat("dd-MM-
           yyyy");
                            date=df.parse(line = br.readLine());



                    while ((line = br.readLine()) != null) {

        if (line.indexOf("Diskgroup/Volume")!= -1) {
                            br.readLine();
                        String ligne = br.readLine();
                        if (ligne!= null) {

                        while (ligne.indexOf("Defined")== -1) {

                        String lo=ligne.trim();
                        Scanner sc =new Scanner(lo);
                       String a="a";
                       String b="b";
                       String c="c";
                       Dmr_dmtool dmrtool = new Dmr_dmtool() ;
                       while (sc.hasNextLine()) {


                                      a=sc.next().toString();
                                      if (sc.hasNext()) {
                                      b=sc.next().toString();}
                                      if (sc.hasNext()) {
                                      c=sc.next().toString();}



                                         dmrtool.setDiskgroup_volume(a);
                                         dmrtool.setMirror_1(b);
                                         dmrtool.setMirror_2(c);
                                         dmrtool.setDate(date);


                                         Dmr_dmtoolService.update(dmrtool);








                       }
                       ligne = br.readLine();
                       }
                }
                        }

                    }
                        }
                }
                       catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (br != null)
                                    br.close();

                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                return "redirect:/logimport/";
            }
 }

错误

观点

【问题讨论】:

标签: java spring spring-mvc controller


【解决方案1】:

您收到此错误的原因是在单击该 A 标记时,它会以 get 方法转到该链接,即使您已在表单中提到作为帖子,因此您可以使用这些方法中的任何一种来解决此问题

解决方案 1

把那个标签换成这样的

<a href="" onclick="document.getElementById('myform').submit();><fmt:messagecode="common.parse" /></a>

并以表格形式放置该链接

<form id="myform" action="c:url value="/logimport/parse" method="post">

解决方案 2

将标签更改为输入标签,以便表单操作和发布方法生效 会被触发

<li><input type="submit"><fmt:message code="common.parse"></input></li>
<form id="myform" action="<c:url value="/logimport/parse" method="post"> <-- in form -->

解决方案 3

写一个JQuery函数就可以实现这个 这是链接Making href (anchor tag) request POST instead of GET?

【讨论】:

    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    相关资源
    最近更新 更多