我正在使用Struts 2.0,Hibernate和JPA。 但是我在jsp页面上遇到了这个strage错误,因此如果您对上述问题有任何解决方案,请帮助我,请找到操作类的详细信息。 在操作类中,我正在获取userRegisterTO对象的值和完整的countryList但在jsp上无法解析userRegisterTO 因此,请帮助我做错了什么。

动作课

public class UserRegistrationAction extends ActionSupport {
    private UserRegisterInterface userRegisterInterface = new UserRegisterService();
    public UserRegisterTO userRegisterTO = null;
    public String registerUser() {
        userRegisterTO = new UserRegisterTO(); 
        this.userRegisterTO = this.userRegisterInterface.listCountryName();
        System.out.println("The size of the list is -  ------ = "+userRegisterTO.getCountryList().size());
        return SUCCESS;
    }

注意-在SOP中,我得到列表的大小,但是在jsp userRegisterTO上无法解析

JSP

<s:form action="/admin/test">
    <s:textfield label="Please enter your name" name="name"/>
    <s:submit/>
    <s:select list="userRegisterTO.countryList" 
        listkey="id" 
        listvalue="countryName"/>
</s:form>

用户注册

public class UserRegisterTO 
{ 
  public List<Country> countryList = new ArrayList<Country>(); 
  public List<Country> getCountryList() 
  { 
   return countryList; 
   } 
  public void setCountryList(List<Country> countryList) { 
    this.countryList = countryList;
   } 

我的动作课

/**
 * 
 */
package com.castle.user.register.action;
import com.castle.user.register.service.UserRegisterInterface;
import com.castle.user.register.service.UserRegisterService;
import com.castle.user.register.service.UserRegisterTO;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Rahul
 *
 */
public class UserRegistrationAction extends ActionSupport {

    private UserRegisterInterface userRegisterInterface = new UserRegisterService();
    private UserRegisterTO userRegisterTO = null;
    public String registerUser(){
         //userRegisterTO = new UserRegisterTO(); 
        this.userRegisterTO = this.userRegisterInterface.listCountryName();
        System.out.println("The size of the list is -  ------ = "+userRegisterTO.getCountryList().size());
        return SUCCESS;
    }
    public UserRegisterTO getUserRegisterTO() {
        return userRegisterTO;
    }
    public void setUserRegisterTO(UserRegisterTO userRegisterTO) {
        this.userRegisterTO = userRegisterTO;
    }


}

我的服务等级

/**
 * 
 */
package com.castle.user.register.service;

import java.util.List;

import com.castle.model.Country;
import com.castle.user.dao.UserRegisterDAO;
import com.castle.user.dao.UserRegisterDAOInterface;

/**
 * @author Rahul
 *
 */
public class UserRegisterService implements UserRegisterInterface{

    private UserRegisterDAOInterface userRegisterDAOInterface = new UserRegisterDAO();

    @Override
    public UserRegisterTO listCountryName() {
        List<Country> countryList = null;
        UserRegisterTO userRegisterTO = new UserRegisterTO();
        try{
            countryList = this.userRegisterDAOInterface.countryList();
            userRegisterTO.setCountryList(countryList);
        }catch(Exception e){
            e.printStackTrace();
        }
        return userRegisterTO;
    }

}

我的道课

/**
 * 
 */
package com.castle.user.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.castle.model.Country;

/**
 * @author Rahul
 *
 */
public class UserRegisterDAO implements UserRegisterDAOInterface {
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    public UserRegisterDAO() {
        this.entityManagerFactory = Persistence
                .createEntityManagerFactory("blogds");
        this.entityManager = entityManagerFactory.createEntityManager();
    }

    @Override
    public List<Country> countryList() {
        List<Country> countryList = null;
        String query =null;
        Query q =null;
        try{
            query = "select c from com.castle.model.Country c";
            countryList = (List<Country>)entityManager.createQuery(query).getResultList();

        }catch(Exception e){
            e.printStackTrace();
        }
        return countryList;
    }

}

action.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>

<package name="user" namespace="/user/registration" extends="struts-default">
    <action name="registerUser" class="com.castle.user.register.action.UserRegistrationAction" method="registerUser">
        <result name="input">/index.jsp</result>
        <result name="success" type="redirect">/jsp/register/register.jsp</result>
    </action>
</package>

</struts>

我的jsp-

<%@include file="/header.jsp"%>
<s:form action="/admin/test">
    <s:textfield label="Please enter your name" name="name"/>
    <s:submit/>
    <s:select list="userRegisterTO.countryList" 
        listkey="id" 
        listvalue="countryName"></s:select>

</s:form>
<%@include file="/footer.jsp"%>

仍然是同样的问题,请提供并解决

===============>>#1 票数:0

您需要为大多数(所有?)Struts 2.0版本提供一个公共获取方法。 访问公共财产的能力后来发生了。

不建议直接成员访问。


编辑发布的新代码

您如何到达input.jsp页面(具有表单的页面)?

我看不到任何将userRegisterTO设置为input结果的代码,这是您看到错误的地方。

您可能会考虑使用prepare拦截器Preparable接口来初始化userRegisterTO

===============>>#2 票数:0

您提供的是model/domain类上的getter and setter 。当动作完成并将其结果发送回UI时,Struts2会将动作及其定义的任何属性放置在values-stack

因此,在您的JSP上,当您尝试使用OGNL访问属性时,框架将尝试根据Value-Stack中的属性来解析它们。

因此,此条目userRegisterTO.countryList将像

getUseruserRegisterTO().getCountryList();

在Short Framework中,将在您的Action类中查找方法getUseruserRegisterTO() ,然后如果解析有效,则将调用getCountryList()

根据您的操作类,我们无法看到任何userRegisterTO getter方法,因此我认为您的操作应类似于

public class UserRegistrationAction extends ActionSupport {
  private UserRegisterInterface userRegisterInterface = new UserRegisterService();
  public UserRegisterTO userRegisterTO = null;
  public String registerUser() {
            userRegisterTO = new UserRegisterTO(); 
            this.userRegisterTO = this.userRegisterInterface.listCountryName();
            return SUCCESS;
        }

public UserRegisterTO  getUserRegisterTO(){
    return this.userRegisterTO ;
} 

//setter for userRegisterTO

如果仍然无法使用,请提供您的configuration file(s)

  ask by Rahul Wagh translate from so

未解决问题?本站智能推荐:

关注微信公众号