用mvc写一个注册程序代码
User.java
package com.mjrg.domain;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private String email;
private Date birthday;
private String nickname;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
XmlUtils.java
package com.mjrg.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlUtils {
private static String filepath;
static{
//得到"users.xml"的路劲
filepath=XmlUtils.class.getClassLoader().getResource("users.xml").getPath();
}
public static Document getDocument() throws Exception{
SAXReader reader=new SAXReader();
Document document=reader.read(new File(filepath));
return document;
}
public static void write2Xml(Document document) throws IOException{
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer=new XMLWriter(new FileOutputStream(filepath),format);
writer.write(document);
writer.close();
}
}
UserDaoImpl.java
package com.mjrg.dao.impl;
import java.text.SimpleDateFormat;
import org.dom4j.Document;
import org.dom4j.Element;
import com.mjrg.domain.User;
import com.mjrg.utils.XmlUtils;
public class UserDaoImpl {
public void add(User user){
try {
Document document=XmlUtils.getDocument();
Element root=document.getRootElement();
Element user_tag=root.addElement("user");
user_tag.setAttributeValue("id", user.getId());
user_tag.setAttributeValue("name", user.getName());
user_tag.setAttributeValue("password", user.getPassword());
user_tag.setAttributeValue("email",user.getEmail());
user_tag.setAttributeValue("birthday", user.getBirthday()==null?"":user.getBirthday().toLocaleString());
user_tag.setAttributeValue("nickname", user.getNickname());
XmlUtils.write2Xml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public User find(String name,String password){
try {
Document document=XmlUtils.getDocument();
Element e=(Element) document.selectSingleNode("//user[@name='"+name+"' and @password='"+password+"']");
if(e==null){
return null;
}
User user=new User();
String date=e.attributeValue("birthday");
if(date==null || date.equals("")){
user.setBirthday(null);
}else{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(sdf.parse(date));
}
user.setEmail(e.attributeValue("email"));
user.setId(e.attributeValue("id"));
user.setName(e.attributeValue("name"));
user.setNickname(e.attributeValue("nickname"));
user.setPassword(e.attributeValue("password"));
return user;
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
//查找注册用户是否存在数据库中
private boolean find(String name){
try {
Document document=XmlUtils.getDocument();
Element e=(Element) document.selectSingleNode("//user[@name='"+name+"']");
if(e==null){
return false;
}
return true;
}catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
UserDaoTest.java(单元测试)
package junit.test;
import java.util.Date;
import org.junit.Test;
import com.mjrg.dao.impl.UserDaoImpl;
import com.mjrg.domain.User;
public class UserDaoTest {
@Test
public void testAdd(){
User user=new User();
user.setBirthday(new Date());
user.setEmail("xxxx@qq.com");
user.setId("45690402");
user.setName("xxxx");
user.setNickname("新叶");
user.setPassword("2354467");
UserDaoImpl dao=new UserDaoImpl();
dao.add(user);
}
}
为什么单元测试老是出现系统找不到路径?路径也对了好几遍可就是错,各位大神求解啊
[解决办法]
users.xml 有没有加载到容器中。
[解决办法]
错误信息贴出来看看
[解决办法]
路径中最好别带空格!
你的路径中 "%20" 就是空格
D:\Program%20File%20(x86) ....
[解决办法]
你把路径复制下来 然后自己手动看看能找到这个文件不, 如果你自己也找不到 说明系统也找不到