struts2 使用ajax 拿不到值;
JSP页面:
JSP页面:
window.onload = function() { $("#oldpwd").blur(function() { // oldpwd失去焦点事件 // 获取参数 var params = { user_name : $("#e_No").val(), user_pwd : $("#oldpwd").val() }; $.ajax({ type : "POST", url : "${pageContext.request.contextPath}/action/userLoginByAjax", data : params, //dataType : "json", // ajax返回值设置为text(json格式也可用它返回,可打印出结果,也可设置成json) success : function(backData, textStatus, ajax) { alert(backData != null ? "get" : "notget"); alert(backData.backinfo); } }); }); };
action:
public class UserLoginAction extends ActionSupport { /**通过spring注入userLoginServiceImpl*/ private UserLoginService userLoginService; /**通过struts2传值*/ private String user_name; private String user_pwd; private String backinfo; /**属性的set/get方法*/ //略........ public String userLoginByAjax(){ /** 验证ajax参数能否传递*/ System.out.println("user_nameajax" + user_name); System.out.println("user_pwdajax" + user_pwd); /** 调用service层登录验证方法*/ List<Employee> employeelist = userLoginService.userLoginValidation(user_name, user_pwd); /** 假如employeelist为空集合,或为null,进入“错误页面” */ if (employeelist.isEmpty() || employeelist==null ) { System.out.println("原密码是错误"); /** ajax回调参数赋值*/ backinfo ="false"; } else { System.out.println("密码正确"); /** ajax回调参数赋值*/ backinfo ="ok"; } return SUCCESS; } }
配置文件:
<!-- struts-ajax.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="ajax" namespace="/" extends="json-default"> <action name="userLoginByAjax" class="action.UserLoginAction" method="userLoginByAjax"> <result name="success" type="json"></result> </action> </package> </struts> <!-- struts.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 配置文件改了以后不用重新启动 管理员模式 --> <constant name="struts.devMode" value="true"/> <!-- 把struts的请求委托给spring管理, 作用:创建Action实例的过程由spring处理,其他的还是有struts2本人处理 --> <constant name="struts.objectFactory" value="spring" /> <include file="struts2/struts-userdb.xml"></include> <include file="struts2/struts-userpermission.xml"></include> <include file="struts2/struts-userlogin.xml"></include> <include file="struts2/struts-ajax.xml"></include> </struts> <!-- web.xml --> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!-- Hibernate OpenSessionInView模式 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- struts2核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
一直拿不到值,如图:
高手帮看看
解决方案
30
看看是不是action少了get set 方法 假如不是这个问题的话浏览器F12看看backData 这个对象里面有什么东西
40
private String backinfo; 需要getter和setter
<result type=”json”>
<param name=”root”>backinfo</param>
</result>
还有你后台传到前台的backinfo不是Json型,而是个String,你前台那样取有问题吧
可参考:http://www.cnblogs.com/lraa/p/3249990.html
<result type=”json”>
<param name=”root”>backinfo</param>
</result>
还有你后台传到前台的backinfo不是Json型,而是个String,你前台那样取有问题吧
可参考:http://www.cnblogs.com/lraa/p/3249990.html
30
沒有格式化json形式,傳值不對吧?你界面打斷點看看