这是UserAction。java代码package com.bjsxt.struts2.user.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {
private int type;

public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} @Override
public String execute() throws Exception {
return "success";
}}这是struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    
    <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
     <result type="redirect">/user_success.jsp?t=${type}</result>
    </action>     
    </package>
    
</struts>
这是index.jsp代码
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%><% String context = request.getContextPath(); %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
向结果传参数
<ol>
<li><a href="user/user?type=1">传参数</a></li>
</ol>

</body>
</html>这是user_success.jsp代码
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>UserSuccess</title>
</head>
<body>
User Success!
from valuestack: <s:property value="t"/><br/>
from actioncontext: <s:property value="#parameters.t"/>
<s:debug></s:debug>
</body>
</html>当我在浏览器输入http://localhost:8080/struts2/
在浏览其中会出现    参数     这两个字
当我点击“参数”这两个字  发生跳转
地址栏中变成http://localhost:8080/struts2/user_success.jsp?t=0
浏览其中出现
User Success! from valuestack: 
from actioncontext:
debug显示的部分信息
__requestWrapper.getAttribute false 
com.opensymphony.xwork2.ActionContext.parameters {=[Ljava.lang.String;@b2e752} 
last.bean.accessed null 
parameters {=[Ljava.lang.String;@b2e752} 
com.opensymphony.xwork2.ActionContext.locale zh_CN 
__link [Ljava.lang.Object;@c5d9c1 
current.property.path null 
session {} 
attr org.apache.struts2.util.AttributeMap@15b4ad2 
我想问的是参数为什么没有取到????这是为什么啊   运行的都很正常啊   web.xml有的 这里我没给出 我想因该不是这里的问题吧   还请明示????

解决方案 »

  1.   

    index.jsp 的问题,
    body之间要写<s:form>,这样才能在action中传值.
      

  2.   

    马士兵老师就是 这样的啊   为什么他可以  我的结果就出不来啊?  我的环境是 jdk5.0   apache-tomcat-6.0.33  myeclipse6.0  我加了<s:form>  这样加的<body><s:form> .....</body>gnl.MethodFailedException: Method "setType" failed for object com.bjsxt.struts2.user.action.UserAction@129c445 [java.lang.NoSuchMethodException: setType([Ljava.lang.String;)]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)
    at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
    at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
    at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
    at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
    at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
    at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:50)2011-12-8 19:22:39 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
    这是后台里输出的信息   还望大家帮我解决一下!!
      

  3.   

    问题到现在已经很明显了,你定义的type是int型的,通过user/user?type=1这种形式传递的参数都是string 型的
      

  4.   

    结果应该是这样:
    User Success! from valuestack: 
    from actioncontext:0"t"没有在action里面定义,Value Stack Contents不会有t,使用<s:property value="t"/>是无法从Value Stack Contents取到值的,所以输出为空白。<s:property value="#parameters.t"/>是从Stack Context里面取值。parameters{t=[Ljava.lang.String;@18cb416},所以<s:property value="#parameters.t"/>,输出0。
      

  5.   

    1 .那我这样的问题 怎样解决呢?? 
    我把int 都改成了 string 还是不好使 2. 并且我别的类似的程序中 例如地址栏中r=1这样取不到数当r=1231就能取到  字母也是 总之赋值一个就是取不到  赋值多个就能取到
      

  6.   

    首先说下,你写的方法可能是老版本了,web.xml配置用的FilterDispatcher过滤器吧,
    现在2.1.8后,官方推荐StrutsPrepareAndExecuteFilter,当然代码是差不多的还有
    地址栏中变成http://localhost:8080/struts2/user_success.jsp?t=0
    url的地址就不对,要加上你的命名空间user不行就把代码发给我,我帮你调试
      

  7.   

    我的这个问题已经不是一两天了  刚一开始时  我觉得是自己的操作问题 后来运行了 一些ognl的时候 就会出现类似的问题  代码我已经贴出来了  还是希望能得到解决的办法   难道是我的环境问题么  这方面我有点弱  大家给出出办法