您的位置:首页 > 编程语言 > Java开发

struts学习笔记—Action实例:保存用户信息到数据库(1)

2013-07-04 18:33 477 查看
1.在struts-config.xml中配置mysql数据源:
<data-sources>
		<data-source type="org.apache.commons.dbcp.BasicDataSource">
			<set-property property="driverClassName"
				value="com.mysql.jdbc.Driver" />
			<set-property property="url"
				value="jdbc:mysql://localhost:3306/struts?characterEncoding=UTF-8" />
			<set-property property="username" value="root" />
			<set-property property="password" value="admin" />
		</data-source>
</data-sources>


2.[b]在struts-config.xml中配置PersonForm与PersonAction:[/b]



<form-beans>
<form-bean name="personForm" type="com.li.form.PersonForm" />
</form-beans>
<action
      attribute="personForm"
      input="/form/addPerson.jsp"
      name="personForm"
      path="/person"
      scope="request"
      type="com.li.struts.action.PersonAction">
      <forward name="success" path="/form/addPersonSuccess.jsp"/>
      <forward name="list" path="/form/listPerson.jsp"/>
</action>


3.Person类代码,Person类封装了人员信息

public class Person {
	private Integer id;
	private String account;
	private String name;
	private Date birthday;
	private Timestamp createDate=new Timestamp(System.currentTimeMillis());
	private boolean secret;
	private List<String> hobby=new ArrayList<String>();
}



4.Action代码:保存用户信息

主方法execute()是一个分发器,根据action参数分发到不同的执行方法。

public class PersonAction extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		PersonForm personForm = (PersonForm) form;// TODO Auto-generated method stub
		if("add".equals(personForm.getAction())){
			return add(mapping,form,request,response);
		}else if("list".equals(personForm.getAction())){
			return List(mapping,form,request,response);
		}
		return mapping.getInputForward();
	}
private ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		PersonForm personForm=(PersonForm) form;	
		// 转化日期
		Date birthday = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(
						personForm.getBirthday()).getTime());
		Person person=new Person();
		person.setAccount(personForm.getAccount());
		person.setName(personForm.getName());
		person.setSecret(personForm.isSecret());
		person.setCreateDate(new Timestamp(System.currentTimeMillis()));//记录用户创建或注册时间;
		person.setBirthday(birthday);
		person.setHobby(Arrays.asList(personForm.getHobby()));//由于Person类里hobby是List<String>类型
		//,所以用List<String> hobby=Arrays.asList(personForm.getHobby());传递数组值。详见com.li.filter.lll.java
		
		PersonDAO personDAO=new PersonDAO();
		Connection conn=getDataSource(request).getConnection();
		personDAO.addPerson(conn,person);//调用DAO层的方法存储person注册的数据
		request.setAttribute("person", person);
		return mapping.findForward("success");
	}
}

其中,Person类里的hobby是List<String>类型,form里是String[ ]数组类型,所以用List<String> hobby=Arrays.asList(personForm.getHobby());传递数组值。

关于如何保存用户注册信息的类personDAO,请看struts学习笔记—Action实例:保存用户信息到数据库(2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐