我正在开发Spring-MVC。我正在使用JSP页面,该页面使用事先自动生成的变量进行调用,我必须将它们复制到控制器中以进行进一步处理。但是它的值是{variablename}。我将发布代码以及我要做的事情。请看看。
控制台输出:
Token value is{token}
Token verification failed
Token in controller is{token}
User and password is :myemail@gmail.com and password checkpassword
控制器代码:
@RequestMapping(value = "/resetpassword")
public String newPasswordPage(Model model){
model.addAttribute("person", new Person());
return "resetpassword";
}
@RequestMapping(value = "/newpassword/{token}")
public String changePasswordFunction(@ModelAttribute("person") Person person, Model model, @PathVariable("token") String token){
if(person.getNewpassword() == null){
return "resetpassword";
} else {
personService.changePassword(token,person.getUsername(),person.getPassword());
System.out.println("Token in controller is"+token);
System.out.println("User and password is :"+person.getUsername()+" and password "+person.getNewpassword());
model.addAttribute("person",person);
return "redirect:/";
}
}
Resetpassword.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<c:url var="addAction" value="/newpassword/{token}" ></c:url>
<form:form action="${addAction}" commandName="person">
<table>
<tr>
<td>
<form:label path="username">
<spring:message text="username"/>
</form:label>
</td>
<td>
<form:input path="username" />
</td>
</tr>
<tr>
<td>
<form:label path="token">
<spring:message text="token"/>
</form:label>
</td>
<td>
<form:input path="token" />
</td>
</tr>
<tr>
<td>
<form:label path="newpassword">
<spring:message text="newpassword"/>
</form:label>
</td>
<td>
<form:input path="newpassword" />
</td>
</tr>
<tr>
<td>
<input name="submit" type="submit" value="Submit" />
</td>
</tr>
</table>
</form:form>
</body>
</html>
答案 0 :(得分:1)
我认为你只是在c:url中错过了你的令牌的$。请尝试
<c:url var="addAction" value="/newpassword/${token}" ></c:url>
答案 1 :(得分:1)
我发现调试此类控制器问题非常有用的一种方法是使用spring MVC Test Framework编写单元测试:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
public class YourControllerTest {
private YourController yourController;
private MockMvc mockMvc;
@Before
public void setup() {
yourController = new YourController();
mockMvc = MockMvcBuilders.standaloneSetup(yourController).build();
}
@Test
public void testChangePasswordFunction() throws Exception {
ResultActions resultActions = mockMvc
.perform(get("/newpassword/myemail@gmail.com:1416653626993:VZelDNRi9pU_ZD6bCzuyFMaJU-Y2pXU7wajMD4I8gtg"));
resultActions.andExpect(status().isFound());
}
}
运行上面的测试会将以下内容输出到控制台(您可能想要注释掉调用personService.changePassword()以简化单元测试设置,或者使用模拟框架来模拟人员服务对象):
Token in controller ismyemail@gmail.com:1416653626993:VZelDNRi9pU_ZD6bCzuyFMaJU-Y2pXU7wajMD4I8gtg
现在,如果你想提取似乎被&#34;分开的变量:&#34;您可以将请求映射方法签名更改为:
@RequestMapping(value = "/newpassword/{token1}:{token2}:{token3}")
public String changePasswordFunction(@ModelAttribute("person") Person person, Model model, @PathVariable("token1") String token1, @PathVariable("token2") String token2, @PathVariable("token3") String token3)