日期:2014-05-17  浏览次数:20712 次

jsp页面传值到action中
jsp页面:
 <c:forEach var="s" items="${news.newsP.list}" varStatus="i" >
 <a href="javascript:void(0);"  onclick="createNewsHtml('${contextPath}/news/createHtml.do?title=${s.title}&content=${s.content}');return false;" >  生成HTML        </a>
</c:forEach>
<script type="text/javascript">
function createNewsHtml(url){
    
     $.ajax( {
    global : false,
type : "get",
url :url+"&random="+Math.random(), 
async : false,
dataType: 'html',
timeout : 5000,
cache: false,

success : function(resp){
  alert("生成成功");
}
});
}
</script>
Action中:
public ActionForward createHtml(){

String title=StrutsEnv.getRequest().getParameter("title");
String content=StrutsEnv.getRequest().getParameter("content");
}
请问下要怎么把title和content的值传到Action当中去,我上面那么做传不到值.

------解决方案--------------------
ajax设置里面type:"GET"大写。提交方式都要大写。有中文的话都转成utf-8
前端都用encodeURI。超链接a标签的onclick的函数里只传url地址不要把参数跟上去。以便于后面好进行encodeURI。onclick改成
 onclick="createNewsHtml('${contextPath}/news/createHtml.do');"
ajax设置中url改成
url:url+"?title="+encodeURI('${title}')+"&content="+encodeURI('${content}')+"&random="+Math.random()

------解决方案--------------------
引用:
引用:createNewsHtml(url)
在方法内部alert一下url、
看看title跟content有值没有!
还有就是get方式提交、注意传值乱码问题!!!
对了 因为content里的内容是<p>   asdfasdfasfdasdf</p>
然后 我传过去的时候会变成  <p>???asdfasdfasfdasdf……

')
你把url参数全部放在JS里面处理!!!
function createNewsHtml(url,title,content){
    url += "?title="+encodeURIComponent(encodeURIComponent(title))+"&content="+encodeURIComponent(encodeURIComponent(content));
     $.ajax( {
    global : false,
type : "get",
url :url+"&random="+Math.random(), 
async : false,
dataType: 'html',
timeout : 5000,
cache: false,

success : function(resp){
  alert("生成成功");
}
});
}
 
后台:
String Name=new String(URLDecoder.decode(request.getParameter("title"),"utf-8"));


在不行就在你的Web容器、假设是Tomcat、找到server.xml文件中找到下面的节点、加上编码 URIEncoding="UTF-8"

<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8"/>