我想在Spring MVC中使用普通的HTML标签,而不是Spring Html标签,但是我无法使用简单的HTML标签将输入值获取到Controller。
下面是我的代码:
控制器:
@Controller
public class LoginService {
@RequestMapping(value = "/authenticate", method = RequestMethod.GET)
public String authenticateUser(Model model,@ModelAttribute("userName") String userName,
@ModelAttribute("password") String password) {
System.out.println("coming in controller " +userName +" : "+ password);
model.addAttribute("message", "Hello Spring MVC Framework!");
return "success";
}
JSP:
<body>
<h2>${message}</h2>
<form action="authenticate.htm">
<table>
<caption>
<h3>Enter Your Login Credentials</h3>
</caption>
<tr>
<td>User Name</td>
<td><input type="text" id="userName"></input></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" id="password"></input></td>
</tr>
<tr rowspan="2">
<td><input type="submit"></input></td>
</tr>
</table>
</form>
web.xml
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
使用RequestParam
代替ModelAttribute
。
@RequestMapping(value = "/authenticate", method = RequestMethod.GET)
public String authenticateUser(@RequestParam("userName") String userName, @RequestParam("password") String password ,Model model) {
System.out.println("coming in controller " +userName +" : "+ password);
model.addAttribute("message", "Hello Spring MVC Framework!");
return "success";
}
您必须在jsp中更改表单的操作网址:
<form action="/authenticate">
使用以下行更新您的web.xml:
<url-pattern>/url-pattern>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句