我正在我的应用程序中实现简单的登录逻辑。我创建了一个login.html和login.js文件。在logins.js中,我正在发出ajax post请求并将userId和密码传递给struts控制器类,但这两个值在我的控制器中都为null。
以下是我的课程:
的index.html
<form id="login-form" name="login-form" method="post" action="#">
<table class="table_signin" >
<!--email address table begins-->
<tr>
<th><label for="email" >Email Address</label></th>
<td><input type="text" id="email" name="email" class="input_width98 text" style="text-transform: lowercase"/></td>
</tr>
<tr>
<th><label for="password" >Password</label></th>
<td><input type="password" id="password" name="password" class="input_width98 text" autocomplete="off"/></td>
</tr>
<tr>
<td><input type="submit" name="login" id="login" value="Submit"/></td>
</tr>
</table>
</form>
MyLogin.js是:
$().ready(function() {
$("#login").click(function(){
var email = $("#email").val();
var password = $("#password").val();
if( email =='' || password =='')
{
$('input[type="text"],input[type="password"]')
$('input[type="text"],input[type="password"]')
alert("Please fill all fields...!!!!!!");
}
else{
var userInfo= {"userEmail":email , "userPassword":password};
alert("else part"+userInfo)
var login = $.ajax({
async: false,
cache: false,
url: "/nts-web/login/login.do",
data: userInfo,
dataType: 'json',
type: 'POST',
crossDomain: true,
success: function (msg) {
var res = msg;
alert("res"+res);
}
});
}
});
});
现在我的struts.xml包含以下配置:
<package namespace="/login" name="login" extends="json-default">
<action name="login" class="com.warnerbros.nts.controller.impl.AdminLoginControllerImpl" method="login">
<interceptor-ref name="json" />
<interceptor-ref name="defaultStack" />
<result type="json">
<param name="root">result</param>
</result>
</action>
</package>
最后我的LoginController是:
public class AdminLoginControllerImpl extends AbstractControllerImpl implements AdminLoginController {
private static final long serialVersionUID = 1L;
@Autowired
private LoginService loginService;
private User userInfo;
public String login() {
System.out.println(userInfo.getUserEmailId());//Here it is throwing null pointer.
String res= "";
return res;
}
}
我有一个像这样的pojo User.java:
public class User {
private String userEmail;
private String userPassword;
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
如何在控制器中获取值useremail和密码?
答案 0 :(得分:0)
有几件事情在上升。
1)如果您需要向操作发送JSON,sed '$b1;/BEGIN/{:1;x;s/\(BEGIN.*END[^\n]*\).*/\1/;t;x;h};H;d' file
可以正确解码。你只定义了JSON拦截器,你没有将它(以及defaultStack)应用到动作中;这样做如下:
you need the JSON Interceptor in your Interceptor Stack
2)为了工作,JSON拦截器要求请求的<action name="login" class="foo.bar.AdminLoginControllerImpl" method="login">
<interceptor-ref name="json" />
<interceptor-ref name="defaultStack" />
<result type="json">
<param name="root">result</param>
</result>
</action>
为contentType
:
application/json
3)您挂载要发送的数据的javascript代码是错误的,不得转义卷曲括号:
...
dataType: 'json',
contentType: 'application/json',
...
4)您的变量名称与页面中的变量名称和操作中的变量名称不匹配。您希望// wrong
var userInfo = '{"userEmail":"'+email+'" , "password":"'+password+'}';
// right
var userInfo = {"userEmail":email , "password":password};
如何匹配userEmail
?或user.userEmailId
到password
?
修复它,它应该可以工作。
答案 1 :(得分:0)
通过发送适当的json对象(如
)解决了这个问题var obj = {"userInfo":{"userEmail":email , "userPassword":password}};
并在下面的struts.xml中是配置。
<package namespace="/login" name="login" extends="json-default">
<interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
<action name="login"
class="com.warnerbros.nts.controller.impl.AdminLoginControllerImpl"
method="login">
<interceptor-ref name="defaultStack" />
<result type="json">
<param name="root">result</param>
</result>
</action>
</package>