小菜学struts2 ,据说编码格式必须为<constant name="struts.i18n.encoding" value="UTF-8"></constant>
但是我写的程序,如果是gbk,可以运行json ,但是如果是utf-8 ,则没反应,奇怪了
配置文件
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
<!-- 定义web应用的默认编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 定义可以动态method模式进行Action访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.ui.templateDir" value="template" />
<constant name="struts.ui.templateSuffix" value="ftl" />
<constant name="struts.locale" value="zh_CN"></constant><!-- 使用ajaxaction处理类的跳转结果 -->
<action name="validName" class="com.hbxyf.action.UserAjaxAction">
<result type="json" />
<interceptor-ref name="defaultStack" />
</action>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>用户添加</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<LINK rel="stylesheet" type="text/css" href="navigation/stylee.css">
<script src="sxhtml/prototype-1.4.0.js" type="text/javascript"></script>
<script src="sxhtml/json.js" type="text/javascript"></script>
<script language="JavaScript">
function validateName()
{
document.getElementById("tip").innerHTML ="";
if(document.getElementById("login").value==""){
return ;
}
//请求的地址
var url = 'validName.action';
//使用Prototype.js函数库来创建请求参数
var params = Form.Element.serialize('login');
//创建Ajax.Request对象,对应于发送请求
var myAjax = new Ajax.Request(
url,
{
//请求方式:POST
method:'post',
//请求参数
parameters:params,
//指定回调函数
onComplete: processResponse,
//是否异步发送请求
asynchronous:true
});
}
function processResponse(request)
{
//使用pareseJSON()方法把服务器响应解析成一个json对象
var action = request.responseText.parseJSON();
//将json对象的tip属性在tip元素中输出
$("tip").innerHTML = action.tip;
}
</script>
<STYLE type="text/css">
.t{
font-family:宋体;
FONT-SIZE: 12px;
}
</STYLE>
</head>
<body>
<jsp:include page="title.jsp"></jsp:include>
<div align="center" style="margin-top: 40px;">
<b>
用户信息设置
</b>
</div>
<s:form action="adduser.action" method="post" theme="simple">
<table align="center" width="40%" border="1" bordercolordark="#ffffff" bordercolorlight="#BDBCC9" cellpadding="0" cellspacing="0" >
<tr onMouseOver="this.bgColor='#cccccc'" onMouseOut="this.bgColor='#ffffff'">
<td align="center">姓名:</td>
<td>
<s:textfield name="user" size="15" cssClass="t"/>
<font style="color: red">*</font>
<s:fielderror cssStyle="color:red;">
<s:param>user</s:param>
</s:fielderror>
</td>
</tr>
<tr onMouseOver="this.bgColor='#cccccc'" onMouseOut="this.bgColor='#ffffff'">
<td align="center" width="30%">
用户名:
</td>
<td align="left">
<s:textfield name="login" size="15" id="login" onblur="validateName()"/>
<font style="color: red">*</font>
<span id="tip" style="color:red;font-weight:bold"></span>
<s:fielderror cssStyle="color:red;">
<s:param>login</s:param>
</s:fielderror>
</td>
</tr>很是奇怪啊,就根本进不到后台,奇怪了
但是我写的程序,如果是gbk,可以运行json ,但是如果是utf-8 ,则没反应,奇怪了
配置文件
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
<!-- 定义web应用的默认编码集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 定义可以动态method模式进行Action访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.ui.templateDir" value="template" />
<constant name="struts.ui.templateSuffix" value="ftl" />
<constant name="struts.locale" value="zh_CN"></constant><!-- 使用ajaxaction处理类的跳转结果 -->
<action name="validName" class="com.hbxyf.action.UserAjaxAction">
<result type="json" />
<interceptor-ref name="defaultStack" />
</action>
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>用户添加</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<LINK rel="stylesheet" type="text/css" href="navigation/stylee.css">
<script src="sxhtml/prototype-1.4.0.js" type="text/javascript"></script>
<script src="sxhtml/json.js" type="text/javascript"></script>
<script language="JavaScript">
function validateName()
{
document.getElementById("tip").innerHTML ="";
if(document.getElementById("login").value==""){
return ;
}
//请求的地址
var url = 'validName.action';
//使用Prototype.js函数库来创建请求参数
var params = Form.Element.serialize('login');
//创建Ajax.Request对象,对应于发送请求
var myAjax = new Ajax.Request(
url,
{
//请求方式:POST
method:'post',
//请求参数
parameters:params,
//指定回调函数
onComplete: processResponse,
//是否异步发送请求
asynchronous:true
});
}
function processResponse(request)
{
//使用pareseJSON()方法把服务器响应解析成一个json对象
var action = request.responseText.parseJSON();
//将json对象的tip属性在tip元素中输出
$("tip").innerHTML = action.tip;
}
</script>
<STYLE type="text/css">
.t{
font-family:宋体;
FONT-SIZE: 12px;
}
</STYLE>
</head>
<body>
<jsp:include page="title.jsp"></jsp:include>
<div align="center" style="margin-top: 40px;">
<b>
用户信息设置
</b>
</div>
<s:form action="adduser.action" method="post" theme="simple">
<table align="center" width="40%" border="1" bordercolordark="#ffffff" bordercolorlight="#BDBCC9" cellpadding="0" cellspacing="0" >
<tr onMouseOver="this.bgColor='#cccccc'" onMouseOut="this.bgColor='#ffffff'">
<td align="center">姓名:</td>
<td>
<s:textfield name="user" size="15" cssClass="t"/>
<font style="color: red">*</font>
<s:fielderror cssStyle="color:red;">
<s:param>user</s:param>
</s:fielderror>
</td>
</tr>
<tr onMouseOver="this.bgColor='#cccccc'" onMouseOut="this.bgColor='#ffffff'">
<td align="center" width="30%">
用户名:
</td>
<td align="left">
<s:textfield name="login" size="15" id="login" onblur="validateName()"/>
<font style="color: red">*</font>
<span id="tip" style="color:red;font-weight:bold"></span>
<s:fielderror cssStyle="color:red;">
<s:param>login</s:param>
</s:fielderror>
</td>
</tr>很是奇怪啊,就根本进不到后台,奇怪了
那你在web.xml配置下看看<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
public class UserAjaxAction extends BaseJumpAction{ private String tip;
private String login; public String getLogin() {
return login;
} public void setLogin(String login) {
this.login = login;
} public String getTip() {
return tip;
} public void setTip(String tip) {
this.tip = tip;
} public String execute() throws Exception {
if(mgr.validateName(login)){
this.setTip("用户名可用 ");
}
else{
this.setTip("用户名已占用 ");
}
return Action.SUCCESS;
}}function processResponse(request)
{
alert("11");
//使用pareseJSON()方法把服务器响应解析成一个json对象
var action = request.responseText.parseJSON();
alert("2")
//将json对象的tip属性在tip元素中输出
$("tip").innerHTML = action.tip;
}
</script>alert(1)执行了,alert(2) 没执行,怎么回事啊?
alert(action);
有没有东西。
LZ,我敬仰你是因为LZ问题解决方法应该还在,求贴。