可以正常跳转到jsp页面,可是无值。于是我在action里写了些用于在控制台上检查值的代码:public class ListUserAction extends ActionSupport {
private userService service;
@Override
public String execute()throws Exception{
Map request = (Map)ActionContext.getContext().get("request");
System.out.println("验证提取:"+this.service.findById(1));
System.out.println("验证提取:"+this.service);
System.out.println("验证提取:"+this);
request.put("list", this.service.findAll());
return SUCCESS;
}
public userService getService() {
return service;
}
public void setService(userService service) {
this.service = service;
}
}
控制台输出为:
验证提取:com.travis.hibernate.DAO.Impl.User@15a2dc4
验证提取:com.travis.service.Impl.userServiceImpl@16fdac
验证提取:com.travis.action.ListUserAction@1ce5e7a如此,我发现this.service.findById(1)这个正常的调用如同其下2个不正常的调用一样,好像显示的是该方法的相关信息..(我也不知道控制台输出的是什么意思)下面跟上配置文件:
web.xml部分  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
  
  <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>
  applicationContext.xml部分<bean id="source" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/travis">
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="source" />
</property>
<property name="mappingResources">
<list>
<value>com/travis/hibernate/DAO/Impl/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

<bean id="transactionManager" 
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="UserDAO" class="com.travis.hibernate.DAO.Impl.UserDAO" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="userServiceTraget"
class="com.travis.service.Impl.userServiceImpl">
<property name="userDao" ref="UserDAO"></property>
</bean>

<bean id="userService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>
<property name="target" ref="userServiceTraget"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<bean id="listUserAction"
class="com.travis.action.ListUserAction">
<property name="service" ref="userService"></property>
</bean>
<bean id="saveUserAction"
class="com.travis.action.SaveUserAction">
<property name="service" ref="userService"></property>
</bean>在线求解...
谢谢!

解决方案 »

  1.   

    我草,CSDN 又抽风了 写了那么多一提交,提示:论坛不存在该贴1。打印this.service.findAll().size()  看看打印出来有没有数据,有可能会NullPointException哦
    2。检查Action的配置,是forward,还是redirect回JSP页面的
    3。如果上面2个都没有问题,那么检查JSP语法。有没有写错。
      

  2.   


    额,好像有希望,用你建议的方法得到:
    验证提取:2
    验证提取:com.travis.hibernate.DAO.Impl.User@113e8f3
    验证提取:com.travis.service.Impl.userServiceImpl@16fdac
    验证提取:com.travis.action.ListUserAction@1ce5e7a可是为什么System.out.println("验证提取:"+this.service.findById(1));
    无法在控制台输出呢?
    额,对,这个返回的是object,我加个getUserName()再试下。谢谢啊,请继续关注啊,哈哈~
      

  3.   

    你用的struts2获取前台的属性不是这样获取的
     Map request = (Map)ActionContext.getContext().get("request");
    Action中没有定义属性,struts2是把属性放到数据共享栈中的
    例如这样
      private String username;
        private String password;    public String getUsername()
    {
            return username;
        }
        public void setUsername(String username)
    {
            this.username = username;
        }    public String getPassword()
    {
            return password;
        }
        public void setPassword(String password)
    {
            this.password = password;
        } public String execute() throws Exception
    {
            if (getUsername().equals("scott")
                    && getPassword().equals("tiger") )
    {
    ActionContext.getContext().getSession().put("user" , getUsername());
                return SUCCESS;
            }
    else
    {
                return ERROR;
            }
        }
      

  4.   


    哈哈,好了!
    System.out.println("验证提取:"+this.service.findAll().size());
    System.out.println("验证提取:"+this.service.findById(1).getUserName());我是这样回jsp的:
    request.put("list", this.service.findAll());
    return SUCCESS;        <action name="listUser" class="listUserAction">
                <result name="success">list.jsp</result>
            </action>
    在jsp页面取值是
    <c:iterator value="#request.list" id="us">
    <tr>
    <td>

    <br></td>
    <td>

    <br></td>
    <td>
    <c:property value="#us.userPassword"/>
    </td>
    </tr>
    </c:iterator>今天弄了一天,都糊涂了,看不出来自己哪里错了都...
      

  5.   


    你的意思是:ActionContext.getContext().getSession().put("user" , getUsername());
    我写的是:ActionContext.getContext().get("request").put("list", this.service.findAll());
    不行了,弄了一天配置,头好大,抽根烟去门外吹下风再回来思考。
    要工作时间好紧,学了3天SSH,现在脑袋都不是我的了。