模型类bean没有被填充

时间:2014-09-03 07:44:27

标签: java jsp struts2 tiles2 model-driven

我写了一个web应用程序,其中有login.jsp,loginAction和login Beans。当我填写表单中的jsp bean时,loginbeans正在通过loginAction填充。当我从这些页面复制逻辑到注册用户时,bean没有被填充。这是我的代码..

<s:form action="login" method="post">
   <p>      <s:textfield    key="username" label="Username" required="true" /><br> 
   <p>  <s:password     key="password" label="Password" required="true" /><br> 
   <p>    <s:submit         value="SignIn"/> <br>            
</s:form>

struts.xml中的相关代码

<action name="login" class="com.ActionClasses.LoginAction">
   <result name="success"   type="tiles">welcome    </result>
   <result name="login" type="tiles">login      </result>
   <result name="input" type="tiles">login      </result>
</action>

LoginAction.java

public class LoginAction  extends ActionSupport implements Action, ModelDriven<LoginBeans>{

LoginBeans loginBeans = new LoginBeans();
.
.
.
    @Override
public LoginBeans getModel() {      
    return loginBeans;
}

LoginBeans.java

public class LoginBeans {
private String username;
private String password;
//getters and setters
}

这一切都很好,豆子正在填补。现在看一下注册用户。

registerOphthalmologist.jsp

<s:form action="registerOphthalmologist" method="post">     
    <s:hidden       key="role"          label="role"    value="O"       />
    <s:textfield    key="username"      label="Choose UserName" required="true"/>
    <s:password     key="password"      label="Choose Password" required="true"/>

    <s:textfield    key="givenName"     label="Name"    required="true"/>
    <s:textfield    key="surname"       label="Surname" required="true"/>
    <s:textfield    key="age"           label="Age"     required="true"     value=""/>
    <s:select       key="gender"        label="Gender"  
                        list="#{'M':'Male','F':'Female'}" 
                        required="true"/>

    <s:textfield    key="contactNumber"     name="contactNumber"    label="Contact Number"  required="true"/>
    <s:textfield    key="email"             name="email"            label="email"           required="true"/>
    <s:textfield    key="city"              name="city"             label="City"            required="true"/>
    <s:textfield    key="district"          name="district"         label="District"        required="true"/>
    <s:textfield    key="state"             name="state"            label="State"           required="true"/>
    <s:textfield    key="country"           name="country"          label="country"         required="true"/>
    <s:textfield    key="postalCode"        name="postalCode"       label="Postal Code"     required="true"/>

    <s:textfield    key="degree"            label="Qualification"           required="true"/>
    <s:textfield    key="university"        label="University"              required="true"     value=""/>
    <s:textfield    key="hospitalName"      label="Hospital Name"           required="true"/>
    <s:textfield    key="experience"        label="Years of Experience"     required="true"     value=""/>

    <s:checkbox     key="checkMe"       label="Agree to terms & Conditions"/>
        <s:submit       value="Register"    align="center"/>

</s:form>

struts.xml中的相关代码

<action name="register*" class="com.ActionClasses.RegisterAction">
   <result name="success"   type="tiles">   home        </result>
   <result name="input" type="tiles">   register{1} </result>
   <result name="error" type="tiles">   register{1} </result>
</action>

RegisterAction.java

public class RegisterAction extends ActionSupport implements Action,ModelDriven<RegisterBeans>
{
RegisterBeans registerBeans = new RegisterBeans();  
.
.
.
    @Override
public RegisterBeans getModel() {
    // TODO Auto-generated method stub
    return registerBeans;
}

RegisterBeans.java

public class RegisterBeans extends ActionSupport{

private String username;
private String password;
private char role;
private boolean checkMe;

private String givenName;
private String surname;
private int age;
private char gender;

private String hospitalName;
private String institute;
private String university;
private String degree;
private int experience;
private int research;
private int ophthalmologists;

private String contactNumber;
private String email;
private String city;
private String district;
private String state;
private String country;
private String postalCode;
//getters and setters
}

这些豆子没有被填满。我在这做什么错?我的完整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>
<package name="default" extends="struts-default">

    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>

    <action name="*Link" method="{1}" class="classes.HomeLinks">
        <result name="home"     type="tiles">home   </result>
        <result name="upload"   type="tiles">upload     </result>
        <result name="register" type="tiles">register   </result>
        <result name="login"    type="tiles">login      </result>
    </action>

    <action name="login" class="com.ActionClasses.LoginAction">
        <result name="success"  type="tiles">welcome    </result>
        <result name="login"    type="tiles">login      </result>
        <result name="input"    type="tiles">login      </result>
    </action>

    <action name="logout" class="com.ActionClasses.LogoutAction">
        <result name="success"  type="tiles">home</result>
    </action>

    <action name="register" class="classes.SpecificRegistrations">
        <result name="Ophthalmologist"  type="tiles">   registerOphthalmologist </result>
        <result name="practitioner"     type="tiles">   registerPractitioner    </result>
        <result name="Researcher"       type="tiles">   registerResearcher      </result>
        <result name="Hospital"         type="tiles">   registerHospital        </result>
        <result name="input"            type="tiles">   register                </result>
    </action>

    <action name="register*" class="com.ActionClasses.RegisterAction">
        <result name="success"  type="tiles">   home        </result>
        <result name="input"    type="tiles">   register{1} </result>
        <result name="error"    type="tiles">   register{1} </result>
    </action>

    <action name="upload" class="com.ActionClasses.UploadAction">
        <interceptor-ref name="fileUpload">
            <param name="maximumSize">3145728</param>
            <param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg, image/jpg</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <result name="success"  type="tiles">   uploaded    </result>
        <result name="error"    type="tiles">   upload      </result>
    </action>
</package>
</struts>

1 个答案:

答案 0 :(得分:1)

潜在错误:

中删除extends ActionSupport
public class RegisterBeans extends ActionSupport{

声明。在某些情况下,它将被视为一种行动,而这不是你想要的。

警告:

  1. 如果您同时指定keylabel,请避免使用value属性,请改用name。根据{{​​3}},它

      

    设置此特定组件的键(名称,值,标签)

  2. 始终在struts配置和<s:form>属性中指定命名空间,仅处理默认命名空间并不好。

  3. 小心required="true"属性;它适用于旧的Struts版本,但在迁移到较新版本the docs后,并且<s:textfield />支持动态属性后,它将在页面上报告与它完全相同(required="true") ,然后由HTML5 required="required"属性等浏览器进行解释,使所有字段成为必填字段,并且前面没有*符号。我昨天在迁移一个旧项目时遇到了这个问题,这真的很烦人。

  4. 为了保持一致性,应该将表示单个bean的实体称为LoginBeanRegisterBean,而不是复数形式LoginBeansRegisterBeans。对于实例变量,只有当它是数组,集合等时才应使用复数形式......

  5. ActionSupport定义为

    public class ActionSupport implements Action, Validateable, ValidationAware, 
                                      TextProvider, LocaleProvider, Serializable {
    

    因此扩展它并实现Action接口是多余的。