主页 > 编程资料 > JSP(JAVA) >
发布时间:2016-01-01 作者:网络 阅读:206次

本文实例讲述了JSP基于dom解析xml的方法。分享给大家供大家参考,具体如下:

初次学习用dom操作xml文件,有很多不足之处,牛人多给点建议,练习时我没对中文做乱码处理,也没做验证哦!O(∩_∩)O~

实体类:User

public class User {
 private String name;
 private String pwd;
 private String email;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getPwd() {
 return pwd;
 }
 public void setPwd(String pwd) {
 this.pwd = pwd;
 }
 public String getEmail() {
 return email;
 }
 public void setEmail(String email) {
 this.email = email;
 }
}

数据访问层接口:UserDao

public interface UserDao {
 boolean login(String name, String pwd);
 void insertUser(User user);
 List selectUser();
 void updateUser(User user);
 boolean deleteUser(String name);
 public User findByName(String name);
}

接口实现类:UserDaoImpl

public class UserDaoImpl implements UserDao {
 private static final String PATH="xml文件路径";
 private void build(Document dom) {
 try {
  //定义转换器
  Transformer f = TransformerFactory.newInstance().newTransformer();
  //设置输出的编码格式
  f.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  //构建dom源
  DOMSource source = new DOMSource(dom);
  //指定文件存放的目标路劲
  StreamResult sr = new StreamResult(new File(PATH));
  //执行转换操作
  f.transform(source, sr);
 } catch (Exception e) {
  e.printStackTrace();
 }
 }
 //登陆
 public boolean login(String name, String pwd) {
 boolean flag = false;
 try {
  //根据以有的xml文件创建Document对象
  Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH));
  //获取user节点下的子节点
  NodeList list = dom.getElementsByTagName("user");
  //遍历list,数据匹配退出
  for(int i = 0; i users = this.selectUser();
  for(int i = 0; i < users.size(); i++){
  //创建节点user
  Element el3 = dom.createElement("user");
  User us =users.get(i);
  //设置节点的属性(name,pwd,email)
  el3.setAttribute("name", us.getName());
  el3.setAttribute("pwd", us.getPwd());
  el3.setAttribute("email", us.getEmail());
  //添加到根节点中
  el.appendChild(el3);
  }
  e2.setAttribute("name", user.getName());
  e2.setAttribute("pwd", user.getPwd());
  e2.setAttribute("email", user.getEmail());
  build(dom);
 } catch (ParserConfigurationException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
 //查询操作
 public List selectUser() {
 List userList = new ArrayList();
 try {
  //根据已有的xml文件创建dom
  Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH));
  //获取所有user节点下的子节点
  NodeList list = dom.getElementsByTagName("user");
  for(int i = 0;i 

业务逻辑层接口:UserService

boolean login(String name, String pwd);
void insertUser(User user);
List selectUser();
void updateUser(User user);
boolean deleteUser(String name);
public User findByName(String name);

接口实现类:UserServiceImpl

public class UserServiceImpl implements UserService {
 UserDao dao = new UserDaoImpl();
 public boolean login(String name, String pwd) {
 return dao.login(name, pwd);
 }
 public void insertUser(User user) {
 dao.insertUser(user);
 }
 public List selectUser() {
 return dao.selectUser();
 }
 public void updateUser(User user) {
 dao.updateUser(user);
 }
 public boolean deleteUser(String name) {
 return dao.deleteUser(name);
 }
 public User findByName(String name) {
 return dao.findByName(name);
 }
}

控制层:UserAction

public class UserAction extends ActionSupport{
  private User user;
 public User getUser() {
 return user;
 }
 public void setUser(User user) {
 this.user = user;
 }
 UserService userService = new UserServiceImpl();
 public String selectUser(){
 HttpServletRequest request = ServletActionContext.getRequest();
 List users = new ArrayList();
 users = userService.selectUser();
 request.setAttribute("USER", users);
 return "select";
 }
 /**
 * 登陆
 * @return
 */
 public String login(){
 if(user.getName() != null && user.getPwd() != null) {
  boolean flag = userService.login(user.getName(), user.getPwd());
  if(flag) {
  return SUCCESS;
  }
 }
 return ERROR;
 }
 /**
 * 修改
 * @return
 */
 public String update(){
 userService.updateUser(user);
 return "update";
 }
 /**
 * 编辑
 * @return
 */
 public String edit(){
 HttpServletRequest request = ServletActionContext.getRequest();
 String name = request.getParameter("uName");
 if(name != null) {
  User u = userService.findByName(name);
  request.setAttribute("USER", u);
 }
 return "edit";
 }
 /**
 * 删除
 * @return
 */
 public String delete(){
 HttpServletRequest request = ServletActionContext.getRequest();
 String name = request.getParameter("uName");
 boolean flag = userService.deleteUser(name);
 System.out.println(flag);
 return SUCCESS;
 }
  /**
   * 添加
   * @return
   */
 public String insert(){
 userService.insertUser(user);
 return "insert";
 }
}

struts.xml配置(本人用的的struts2):




  
   
    /list.jsp
   
   
    /list.action
    /login.jsp
   
   
    /list.action
   
   
    /list.action  
   
   
    /list.action  
   
   
    /update.jsp  
   
  

web.xml配置



 
  index.jsp
 
 
  struts2
  
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
 
 
  struts2
  /*
 

四个页面:login.jsp  list.jsp insert.jsp,update.jsp

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 
  
  My JSP 'login.jsp' starting page
 
 
   
 
 
 
 
 
  首页|注册
  
用户名:
密码:

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 
  
  My JSP 'list.jsp' starting page
 
 
   
 
 
 
 
 
 注册
  
    
用户列表
序号用户名密码邮箱删除编辑
${temp.index+1}${u.name }${u.pwd }${u.email}删除编辑

insert.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
  + request.getServerName() + ":" + request.getServerPort()
  + path + "/";
%>


 
 
 My JSP 'register.jsp' starting page
 
 
 
 
 
 
 
 
 首页
 
用户名:
密码:
邮箱:

update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 
  
  My JSP 'update.jsp' starting page
 
 
   
 
 
 
 
 
  首页
  

更新操作

用户名:
密码:
邮箱:

本人学习所用的user.xml文件







希望本文所述对大家jsp程序设计有所帮助。

关键字词: