使用Hibernate将下拉列表值保存到Struts 2中的数据库 [英] Save drop-down list value to database in Struts 2 with Hibernate

查看:147
本文介绍了使用Hibernate将下拉列表值保存到Struts 2中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



第一个 index.jsp

我想将选定的下拉列表值保存到数据库中。 >被加载。从 index.jsp ,当我们点击的注册URL时,我们可以转到 register.jsp index.jsp


$ b struts.xml

 < action name =registerActionclass =action.RegisterActionmethod =populateSelect> 
< result name =success> register.jsp< / result>

< / action>
< result name =success> login.jsp< / result>

< / action>

index.jsp

 < s:url id =urlaction =registerAction> 
< / s:url>
< s:a href =%{url}>注册< / s:a>

register.jsp

 < s:form action =registerActionmethod =execute> 
< s:submit value =Register/>
< / s:form>

操作类是:

  public class RegisterAction extends ActionSupport {

字符串名称,密码,电子邮件,地址,月;

int phno;


列表<月> allMonths = new ArrayList< Month>();
列表<用户> users = new ArrayList< User>();
UserDao udao = new UserDao();


公共列表<月> getAllMonths(){
返回allMonths;
}

public void setAllMonths(List< Month> allMonths){
this.allMonths = allMonths;
}

public String getMonths(){
return months;
}

public void setMonths(String months){
this.months = months;
}

公共列表<用户> getUsers(){
返回用户;
}

public void setUsers(List< user> users){
this.users = users;
}


public String getAddress(){
return address;
}

public void setAddress(String address){
this.address = address;
}

public String getEmail(){
return email;
}

public void setEmail(String email){
this.email = email;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public int getPhno(){
return phno;
}

public void setPhno(int phno){
this.phno = phno;
}

public String getPwd(){
return pwd;
}

public void setPwd(String pwd){
this.pwd = pwd;

$ b $ public bb



$ public b




$ b $新用户();
u.setName(name);
u.setEmail(email);
u.setAddress(address);
u.setPhno(phno);
u.setPwd(pwd);
System.out.println(Hi der+ months);

u.setMonths(months);
udao.addUser(u);
返回成功;
}

public String listAllUsers(){
users = udao.getUsers();
System.out.println(In Action,+ users);
返回成功;



public String populateSelect(){
allMonths = udao.getMonths();
System.out.println(In constructor+ allMonths);
返回成功;
}
}

下拉式列表其实只有一种形式领域。还有其他领域的形式。
月份字段以外的所有值都可以输入到数据库中。对于月份字段,输入的值为空。



我认为没有采用下拉式的值。

个月成员变量更改为 Map c>像这样:

 私人地图< Integer,String> months = new HashMap< Integer,String>(); 

然后,在您的实现 Preparable 界面动作,并用这样的东西初始化地图:

  public void prepare()throws Exception {
String [] monthNames = new DateFormatSymbols()。getMonths();
int i = 1;
for(String monthName:monthNames){
months.put(i ++,monthName);


当然,你需要添加一个getter和一个setter for months



另外,添加一个私人整数月份你的动作中的成员变量,它将持有用户选定的月份(同样,用getter和setter)

然后,使用下面的 s:在JSP中选择标记:

 < s:select label =Select Date of Monthname =monthheaderKey =0
headerValue = - Select--list =months/>

现在在 execute 方法中,月份成员变量应该保存选定的月份。 0应该是没有选择的,1应该是1月份等。


I want to save the selected value of drop-down list into the database.

First index.jsp is loaded. From index.jsp, we can go to register.jsp when we click register URL of index.jsp.

struts.xml:

<action name="registerAction" class="action.RegisterAction" method="populateSelect">
        <result name="success" >register.jsp</result>

    </action>
    <action name="register" class="action.RegisterAction" method="execute">
        <result name="success" >login.jsp</result>

    </action>

index.jsp:

  <s:url id="url" action="registerAction">
</s:url>
  <s:a href="%{url}">Register</s:a>

register.jsp:

   <s:form action="registerAction" method="execute">
     <s:select label="Select Date of Month" key="Month List" name="months" headerKey="0" headerValue="--Select--" list="allMonths" listKey="id" listValue="name"/>
 <s:submit value="Register"/>
 </s:form>

Action class is:

public class RegisterAction extends ActionSupport {

    String name, pwd, email, address, months;

    int phno;


    List<Month> allMonths = new ArrayList<Month>();
    List<User> users = new ArrayList<User>();
    UserDao udao = new UserDao();


public List<Month> getAllMonths() {
    return allMonths;
}

public void setAllMonths(List<Month> allMonths) {
    this.allMonths = allMonths;
}

public String getMonths() {
    return months;
}

public void setMonths(String months) {
    this.months = months;
}

public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}


public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPhno() {
    return phno;
}

public void setPhno(int phno) {
    this.phno = phno;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}

    public RegisterAction() {
    }


    public String execute() throws Exception {
        User u = new User();
        u.setName(name);
        u.setEmail(email);
        u.setAddress(address);
        u.setPhno(phno);
        u.setPwd(pwd);
        System.out.println("Hi der "+months);

        u.setMonths(months);
        udao.addUser(u);
        return "success";
    }

    public String listAllUsers() {
        users = udao.getUsers();
        System.out.println("In Action, " + users);
        return "success";
    }


    public String populateSelect() {
        allMonths = udao.getMonths();
        System.out.println("In constructor " + allMonths);
        return "success";
    }
}

the drop down list is actually only one of the form fields. there are other fields also in the form. All values other than month field can be entered into database. for month field, the value being entered is null.

I think the value of drop-down is not being taken.

解决方案

I would suggest to change the months member variable to a Map like this:

private Map<Integer, String> months = new HashMap<Integer, String>();

Then, implement the Preparable interface in you action, and initialize the map with something like this:

public void prepare() throws Exception {
    String[] monthNames = new DateFormatSymbols().getMonths();
    int i = 1;
    for (String monthName : monthNames) {
        months.put(i++, monthName);
    }
}

Of course, you need to add a getter and a setter for months

Also, add an private Integer month member variable in your action, that will hold the selected month by the user (again, with getters and setters)

Then, use the following s:select tag in your JSP:

<s:select label="Select Date of Month" name="month" headerKey="0"
          headerValue="--Select--" list="months"/>

So now in your execute method, the month member variable should hold the selected month. 0 should be no selection, 1 should be January, etc.

这篇关于使用Hibernate将下拉列表值保存到Struts 2中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆