这是我的action文件package com.xh.action;
import java.util.HashMap;
import java.util.Map;import com.opensymphony.xwork2.ActionSupport;
import com.xh.pojo.Employee;
import com.xh.service.DAO.EmpServiceDAO;public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Integer employeeid;
private String password;
private EmpServiceDAO empService;
private Map<String,String> result = new HashMap<String, String>();
public Map<String, String> getResult() {
return result;
}
public void setResult(Map<String, String> result) {
this.result = result;
}
public EmpServiceDAO getEmpService() {
return empService;
}
public void setEmpService(EmpServiceDAO empService) {
this.empService = empService;
} public Integer getEmployeeid() {
return employeeid;
}
public void setEmployeeid(Integer employeeid) {
this.employeeid = employeeid;
} public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception { Employee emp = empService.findById(employeeid) ;
if(emp == null){
result.put("result", "雇员不存在");
}else{
if(emp.getPassword().equals(password)){
result.put("result", "ok") ;
}else{
result.put("result", "密码不正确") ;
}
}
return SUCCESS;
}
}
这是我的jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript" src="script/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submit").click(function(){
var params={
employeeid:$("#employeeid").val(),
password:$("#password").val()
};
$.ajax({
url:"LoginAction.action",
data: params,
type:"POST",
dataType:"text",
contentType:"application/x-www-form-urlencoded;charset=utf-8",
success:function(response){
alert(response);
var jsonObj = $.parseJSON(response);
alert(jsonObj);
if(jsonObj.result.result=="ok"){
location.href="<%=path%>/home.jsp";
}else{
if(jsonObj.result.result == "雇员不存在"){
$("#warn").html(jsonObj.result.result);
}
else{
$("#warn2").html(jsonObj.result.result);
}
$("#employeeid").val("");
$("#password").val("");
}
}
});
});
});
</script>
</head>
<body> <input type="text" name="employeeid" id="employeeid"><span id="warn"></span><br>
<input type="password" name="password" id="password"><span id="warn2"></span><br>
<input type="submit" value="登录" id="submit">
<s:debug></s:debug>
</body>
</html>这是我的beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经不用的连接慢慢释放一部分 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值,当空闲的连接数小于该值时,连接池会预申请一些连接 -->
<property name="minIdle" value="1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/xh/pojo/Department.hbm.xml</value>
<value>com/xh/pojo/Employee.hbm.xml</value></list>
</property>
</bean>
<bean id="empServiceImp" class="com.xh.sevice.implement.EmpService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="depServiceImp" class="com.xh.sevice.implement.DepService">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="LoginAction" class="com.xh.action.LoginAction" scope="prototype">
<property name="empService" ref="empServiceImp"></property>
</bean>
</beans>求哪位大神帮我看看前台获得的json数据居然是一个html代码,看得我晕了
1.你strust不应该返回success,
2再说,你strust都不是返回json,
3再再说,你返回的数据应该通过response反馈回去
以上3步都错了,所以才会出现你那种情况,呵呵~
response.setContentType("application/json; charset=UTF-8");
response.getWriter().write(“要返回给页面的信息,必须按json数据格式拼好,可以参考net.sf.json.JSONObject此类”);
现在出了一个新问题,就是我将js代码提出来放到了一个新的文件中有不行了。我猜是$.ajax({url:"LoginAction.action"})已经success的回调函数中的location.href出了错误,求解决方案