从jsp传递参数到springboot控制器
Posted
技术标签:
【中文标题】从jsp传递参数到springboot控制器【英文标题】:pass parameter from jsp to springboot controller 【发布时间】:2019-04-01 22:04:40 【问题描述】:我正在尝试将密码值传递给 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 仅供参考。
【问题讨论】:
memorynotfound.com/… 按照本教程进行操作。 【参考方案1】:所以,你有一些选择:
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
我认为这有助于您在请求中发送参数。
【讨论】:
感谢您的快速回复,现在我收到Neither BindingResult nor plain target object for bean name 'user' available as request attribute
这是因为您没有创建资源来设置您的对象具有模板中的属性。在你的控制器内部创建一个像这样的方法:@RequestMapping("/login") public String loginPage(Model model) model.addAttribute("user", new User()); return "login";
在你的模板对象中设置。但是,请记住,如果您正确返回页面,它会起作用。这意味着如果您输入(例如:http:/localhost:8080/login),该页面将返回您的登录页面。以上是关于从jsp传递参数到springboot控制器的主要内容,如果未能解决你的问题,请参考以下文章
spring boot中三个jsp页面之间怎么传递参数?第一个页面传值,第三个页面怎么获取参数?<%%>获取不到参数