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 blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.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) */@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {// TODO Auto-generated method stubSystem.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" onsubmit="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是配置成功了的。求解答,谢谢~~~