开发者

Struts: Not populating fields using mutators in form

开发者 https://www.devze.com 2023-01-04 06:15 出处:网络
I\'m trying to use struts for the first time, and something is not right in my setup.I\'ve got an Action Form called TeamForm with the following contents:

I'm trying to use struts for the first time, and something is not right in my setup. I've got an Action Form called TeamForm with the following contents:

package struts.forms;

import org.apache.struts.action.ActionForm;

import domain.TeamBean;

public class TeamForm extends ActionForm {

private TeamBean teamBean = new TeamBean();

public TeamBean getTeamBean(){ return teamBean; }

public void setTeamName(String teamname) { teamBean.setTeamName(teamname); } public String getTeamName() { return teamBean.getTeamName(); }

public void setMember1(String member1) { teamBean.setMember1(member1); } public String getMember1() { return teamBean.getMember1(); }

public void setMember2(String member2) { teamBean.setMember2(member2); } public String getMember2() { return teamBean.getMember2(); }

public void setMember3(String member3) { teamBean.setMember3(member3); } public String getMember3() { return teamBean.getMember3(); }

public void setMember4(String member4) { teamBean.setMember4(member4); } public String getMember4() { return teamBean.getMember4(); }

public void setMember5(String member5) { teamBean.setMember5(member5); } public String getMember5() { return teamBean.getMember5(); } }

I have an Action called CreateTeamAction with the following: package struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import managers.CreateTeamMgr;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import struts.forms.TeamForm;
import domain.TeamBean;
import exceptions.ServiceLoadException;

public class CreateTeamAction extends Action { 

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

try { TeamBean teamBean = ((TeamForm)form).getTeamBean(); boolean validTeam = (new CreateTeamMgr().authentic开发者_JS百科ate(teamBean));

request.setAttribute("team", teamBean); if(validTeam) { System.out.println("Forwarding to teamSuccess..."); return mapping.findForward("teamSuccess"); } else { System.out.println("Forwarding to teamFailure..."); return mapping.findForward("teamFailure"); } } catch (ServiceLoadException e) { System.out.println("Forwarding to failure..."); e.printStackTrace(); return mapping.findForward("teamFailure"); }

} }

The applicable parts of my struts-config.xml are: (Please note, the " before the opening struts-config tag is not part of my file. I had to add it to make the xml show up in the preview window.):

"<struts-config
  <form-beans
    <form-bean
      name="teamForm"
      type="struts.forms.TeamForm"/>    
  </form-beans>
  <action-mappings>
    <action 
      path="/team"
      type="struts.actions.CreateTeamAction"
      name="teamForm"
      scope="request">
      <forward name="success" path="/teamHome.jsp"/>
      <forward name="failure" path="/teamError.jsp"/>
    </action>
  </action-mappings>
</struts-config>

As I understand it, Struts should be using the setters in my TeamForm to populate the values for the TeamBean. Unfortunately, this isn't happening. When I try to run, all the member data for the TeamBean is set to null. Can anyone see what I'm missing?

Thanks!


It's working now, but I'm not sure why the change I made helped. Maybe someone with more experience can tell me...

I originally had a member data field called teamName, so I named the mutator setTeamName(). I changed the data field to be teamname (no capital N) and the corresponding mutator to setTeamname. That change made everything work.

As I understand the Java naming standards, teamName and setTeamName() should have been perfectly valid names, so I don't understand why changing the capital N to a lower-case N changed the behavior I was seeing. Can anyone explain this to me?

Thanks!


What did your JSP look like? If your JSP had an input with the property "teamname" then changing the ActionForm to a lower case n would suddenly work since the bean properties are case sensitive. If you want your ActionForm's field to be "teamName" then the property in the JSP must be named the same way. I'm not sure if that's your issue or not without seeing your JSP, but it's my first guess.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号