Struts-1 表单传值

初学Struts时,表单传值,当一个jsp写好后,从Action中得到的始终是null。不明白是什么原因?

ActionForm代码:

package edu.swu.hyc.form;

import org.apache.struts.action.ActionForm;

public class UserInfoActionForm extends ActionForm {
	private String age;
	private String name;
	private String profession;
	private String sex;
	/**
	 * @return the age
	 */
	public String getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(String age) {
		this.age = age;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the profession
	 */
	public String getProfession() {
		return profession;
	}
	/**
	 * @param profession the profession to set
	 */
	public void setProfession(String profession) {
		this.profession = profession;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

 Action代码:

package edu.swu.hyc.form;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UserInfoAction extends Action {
	private final String dbDriver = "com.mysql.jdbc.Driver";
	private final String url = "jdbc:mysql://localhost:3306/test";
	private final String user = "root";
	private final String password = "root";
	private Connection con = null;
	
	public UserInfoAction(){
		try {
			Class.forName(dbDriver).newInstance();
			con = DriverManager.getConnection(url, user, password);
			con.setAutoCommit(true);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean executeUpdate(UserInfoActionForm form){
		String sql = "insert into user values(?,?,?,?)";
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, form.getName());
			pst.setString(2, form.getAge());
			pst.setString(3, form.getSex());
			pst.setString(4, form.getProfession());
			pst.executeUpdate();
			con.close();
			pst.close();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
		
	}

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
			System.out.println("---------------------------------");
		UserInfoActionForm userInfoActionForm = (UserInfoActionForm)form;
		userInfoActionForm.setName(userInfoActionForm.getName());
		userInfoActionForm.setAge(userInfoActionForm.getAge());
		userInfoActionForm.setSex(userInfoActionForm.getSex());
		userInfoActionForm.setProfession(userInfoActionForm.getProfession());
	
		String message = "添加用户失败";
		if (executeUpdate(userInfoActionForm)){
			message = "添加用户成功";
		}
		
		request.setAttribute("messger", message);
		con.close();
		
		return mapping.findForward("insertUserInfo");
	}

	
}

 Jsp代码:

<body>
    <form name="form" method="post" action="UserInfoAction.do" οnsubmit="return mycheck()">
    	<table width="481" border="0" align="center">
    		<tr>
    			<td width="60" height="30">姓名</td>
    			<td width="166" height="30"><input type="text" name="name" /></td>
    			<td width="60" height="30">年龄</td>
    			<td width="166" height="30"><input type="text" name="age" /></td>
    		</tr>
    		<tr>
    			<td width="60" height="30">性别</td>
    			<td width="166" height="30"><input type="text" name="sex" /></td>
    			<td width="60" height="30">职业</td>
    			<td width="166" height="30"><input type="text" name="profession" /></td>
    		</tr>
    	</table>
    	<br/>
    	<div align="center">
    		<input type="submit" name="submint" value="添加"/>
    	</div>
    </form>
    
    <%
    	if(request.getAttribute("messger") != null)
    		out.println(request.getAttribute("messger"));
     %>
  </body>

 Struts-Config代码:

<form-beans>
		<form-bean name="userInfoActionForm" type="edu.swu.hyc.form.UserInfoActionForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/userInfoAction" name="userInfoAction" scope="request" type="edu.swu.edu.hyc.form.UserInfoAction"
				validate="true">
			<forward name="inserUserInfo" path="/index.jsp"></forward>		
		</action>
	</action-mappings>
	
	<message-resources parameter="ApplicationResources"></message-resources>

 这个是插入到MysqL中,但从Action中得到的Form值为null,不明白是为什么?Mysql是配置成功了的。求解答,谢谢~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值