Java 将参数从jsp传递到springboot控制器

Java 将参数从jsp传递到springboot控制器,java,spring,jsp,spring-boot,spring-security,Java,Spring,Jsp,Spring Boot,Spring Security,我正试图将密码值传递给spring boot controller,但没有得到任何响应。有人能给我一些建议吗 我之前使用的是SpringMVC控制器,这是我第一次尝试SpringBoot <form method="POST" th:action="@{/esparkUserPage}"> <div class="control-group"> <input id="password"

我正试图将密码值传递给spring boot controller,但没有得到任何响应。有人能给我一些建议吗

我之前使用的是SpringMVC控制器,这是我第一次尝试SpringBoot

<form method="POST" th:action="@{/esparkUserPage}">
                        <div class="control-group">
                 <input id="password" type="password" name="password" class="form-control input-sm" required="User Pwd is Required" placeholder="password"  />
                <label class="login-field-icon fui-lock" for="login-pass"></label>
                </div>
                <input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" id="submit" name="submit" />
                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                </form>
Securityconfig:

 @Override
protected void configure(HttpSecurity httpSecurity) throws Exception {

    httpSecurity
            .authorizeRequests()
            //.antMatchers("/", "/esparkLoginPage","/passReset").permitAll()
            .anyRequest()
            .permitAll() //.authenticated()
            .and()
            .formLogin()
            .loginPage("/esparkLoginPage")
            .defaultSuccessUrl("/esparkUserPage")
            .permitAll()
            .and()
            .csrf().disable()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/esparkLoginPage")
            .permitAll();

}
重置控制器

    @Controller
public class ResetController {
    @ResponseBody
    @RequestMapping(value = "/esparkUserPage", method = RequestMethod.POST)
    public String esparkUserPage(HttpServletRequest httpRequest,HttpServletResponse response ) {


        String username = httpRequest.getParameter("username");
        String password = httpRequest.getParameter("password");
        System.out.println(username);
         List<String> result = new ArrayList<String>();
        /*     try
             {

                 JSch jsch = new JSch();


                 System.out.println("Inside shell");
                 Session session = jsch.getSession(USERNAME, host, port);
                 session.setConfig("StrictHostKeyChecking", "no");
                 session.setPassword(PASSWORD);
                 session.connect();

                 //create the excution channel over the session
                 ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

                 System.out.println(channelExec.toString());
                 // Gets an InputStream for this channel. All data arriving in as messages from the remote side can be read from this stream.
                 InputStream in = channelExec.getInputStream();
                 String com="sh set_passwd"+" "+username+" "+"'"+password+"'" ;
                 channelExec.setCommand(com);
                 channelExec.connect();
                 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                 String line;
                 while ((line = reader.readLine()) != null)
                 {
                     result.add(line);
                 }
                 int exitStatus = channelExec.getExitStatus();
                 channelExec.disconnect();
                 session.disconnect();

                 if(exitStatus < 0){
                 }
                 else if(exitStatus > 0){
                 }
                 else{
                 }

             }
             catch(Exception e)
             {
                 System.err.println("Error: " + e);
             }*/
             return "Password Reset Done!";
         }


}
@控制器
公共类重置控制器{
@应答器
@RequestMapping(value=“/esparkUserPage”,method=RequestMethod.POST)
公共字符串esparkUserPage(HttpServletRequestHttpRequest,HttpServletResponse){
字符串username=httpRequest.getParameter(“用户名”);
字符串密码=httpRequest.getParameter(“密码”);
System.out.println(用户名);
列表结果=新建ArrayList();
/*试一试
{
JSch JSch=新的JSch();
System.out.println(“内壳”);
Session Session=jsch.getSession(用户名、主机、端口);
session.setConfig(“StrictHostKeyChecking”、“no”);
session.setPassword(密码);
session.connect();
//在会话上创建执行通道
ChannelExec ChannelExec=(ChannelExec)session.openChannel(“exec”);
System.out.println(channelExec.toString());
//获取此通道的InputStream。作为消息从远程端到达的所有数据都可以从此流读取。
InputStream in=channelExec.getInputStream();
字符串com=“sh set_passwd”+“”+用户名+“”+“”+密码+”;
channelExec.setCommand(com);
channelExec.connect();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(in));
弦线;
而((line=reader.readLine())!=null)
{
结果。添加(行);
}
int exitStatus=channelExec.getExitStatus();
channelExec.disconnect();
session.disconnect();
if(exitStatus<0){
}
否则如果(exitStatus>0){
}
否则{
}
}
捕获(例外e)
{
System.err.println(“错误:+e”);
}*/
返回“密码重置完成!”;
}
}
另外,请给出从spring boot执行shell命令的最佳方法的建议?
第一次使用spring boot供参考。

因此,您有一些选择:

1-如果您只需要将密码作为字符串传递,您可以在控制器中使用注释
@RequestParam
发送您的请求,因此控制器必须看起来像:

   @Controller
   public class ResetController {

      @ResponseBody
      @RequestMapping(value = "/esparkUserPage", method = RequestMethod.POST)
      public String esparkUserPage(@RequestParam("password")String password, HttpServletRequest httpRequest,HttpServletResponse 
      response ) {
           // your code
      }
2-如果要为控制器发送对象,一种方法可能是: 创建一个资源来打开模板,这意味着,只需在登录页面中添加引用对象,如下所示:

//supose your login.html
    @RequestMapping("/login")
    public String loginPage(Model model) {
        model.addAttribute("user", new User());
        return "login";
    }
       <form method="POST" th:action="@{/esparkUserPage}" th:object="${user}">
                    <div class="control-group">
             <input id="password" type="password" name="password" class="form-control input-sm" required="User Pwd is Required" placeholder="password" th:field="*{password}" />
            <label class="login-field-icon fui-lock" for="login-pass"></label>
            </div>
            <input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" id="submit" name="submit" />
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
       </form>
然后确保模板正确绑定对象,如下所示:

//supose your login.html
    @RequestMapping("/login")
    public String loginPage(Model model) {
        model.addAttribute("user", new User());
        return "login";
    }
       <form method="POST" th:action="@{/esparkUserPage}" th:object="${user}">
                    <div class="control-group">
             <input id="password" type="password" name="password" class="form-control input-sm" required="User Pwd is Required" placeholder="password" th:field="*{password}" />
            <label class="login-field-icon fui-lock" for="login-pass"></label>
            </div>
            <input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" id="submit" name="submit" />
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
       </form>
}

现在在控制器上添加如何在服务器端绑定对象的注释:

  @ResponseBody
  @RequestMapping(value = "/esparkUserPage", method = RequestMethod.POST)
  public String esparkUserPage(@ModelAttribute(name="user") User user, HttpServletRequest httpRequest,HttpServletResponse 
  response ) {
       // your code
  }

我认为这有助于您在请求中发送参数。

感谢您的快速响应,现在我得到的
既不是BindingResult,也不是bean name“user”的普通目标对象,都可以作为请求属性使用
,这是occour,因为您没有创建资源来设置您的对象具有来自模板的属性。在控制器内部创建如下方法:
@RequestMapping(“/login”)公共字符串loginPage(Model Model){Model.addAttribute(“user”,new user());返回在模板上的对象中设置的“login”}
。但是,请记住,如果您正确返回页面,它就可以工作。这意味着如果您使用(例如:http:/localhost:8080/login)输入,页面将返回您的登录页面。遵循本教程。