我正在使用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>
UserRegisterTO
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"%>
问题仍然存在,请提供并解决
答案 0 :(得分:0)
你需要为大多数(全部?)版本的Struts 2.0提供一个公共getter方法;访问公共财产的能力在以后发生。
不建议直接访问成员。
编辑新发布的代码
如何到达input.jsp
页面,页面为?
我没有看到任何为userRegisterTO
结果设置input
的代码,这就是您看到错误的位置。
您可以考虑使用prepare
interceptor和Preparable
interface初始化userRegisterTO
。
答案 1 :(得分:0)
您在getter and setter
课程中提供的是model/domain
。当操作完成其工作并将结果发送回您的UI时,Struts2会将操作及其任何属性定义在values-stack
中的操作类。
因此,当您尝试使用OGNL访问属性时,您的JSP将尝试针对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)
。