繁体   English   中英

将参数从 jsp 传递给 springboot 控制器

[英]pass parameter from jsp to springboot controller

我正在尝试将密码值传递给 spring-boot 控制器,但没有得到任何回应有人可以就此提出建议吗?

我之前在使用 spring MVC 控制器,第一次尝试 spring-boot

<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>

MVc配置:

    @Configuration
public class WbMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("esparkLoginPage");
        registry.addViewController("/esparkHome").setViewName("esparkHome");
        registry.addViewController("/esparkUserPage").setViewName("esparkUserPage");
        registry.addViewController("/esparkLoginPage").setViewName("esparkLoginPage");

    }

            }

安全配置:

 @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!";
         }


}

还请就从 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>

这是您的用户类的模型:

import java.io.Serializable;

public class User implements Serializable {
   private static final long serialVersionUID = 1L;
   private String username;
   private String password;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

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

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

我认为这有助于您根据请求发送参数。

暂无
暂无

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

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