私は自分のアプリケーションに単純なログインロジックを実装しています。 login.htmlとlogin.jsファイルを作成しました。 logins.jsで私は、AJAX投稿要求を作成し、ユーザーIDとパスワードをstrutsコントローラクラスに渡していますが、両方の値がコントローラでnullです。以下はstruts2コントローラクラスのajax投稿要求パラメータにアクセスできない
は私のクラスです:
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>
最後にm
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があります:yのLoginControllerがあり、コントローラの値がUSEREMAILとパスワードを取得する方法
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;
}
を?
interceptor-refの名前は何ですか?私はどんな名前をつけてもいいですか? – user3541321
O.o名前を付けることはできません。 'Interceptor-ref'では' interceptor'を 'ref 'しているので、参照しているインターセプタの名前を使う必要があります。通常はdefaultStackを使用する必要があります。これは内部的にすべてのデフォルトのインターセプタを持つスタックです。 JSPからJSONデータをアクションに送信する必要がある場合は、JSONインターセプタも使用する必要があります。次に、私が書いたもの、つまり「jsonインターセプタを使用して、他のすべてのデフォルト・インターセプタを使用する」をコピー・ペーストします。 –
また、json-defaultを拡張しているので、タグのインターセプタを定義する必要はありません。 –