本篇文章介绍如何使用Struts1
进行增删改查,仅是对自己学习的记录,并没有深入分析每个模块(不重复造轮子O(∩_∩)O~)。
开源的Web应用服务器,apache-tomcat-8.0.35-windows-x64.zip仅有10.3M。启动服务:bin-startup.bat。
建立数据库test
,并在其中建立表users
。
CREATE DATABASE test
USE test
CREATE TABLE users (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(10) NOT NULL,
sex varchar(2) NOT NULL,
phone varchar(11) DEFAULT NULL,
address varchar(50) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8;
查看表users
DESC users
数据库管理工具-Navicat
采用Java Servlet/JSP,实现了基于Java Web应用的MVC框架,是MVC的经典产品。
struts-config.xml:声明和映射Struts组件。
将lib目录下,所有的jar包复制到工程目录WEB-INF的lib目录中。
在web.xml DD配置文件中,加入
action
org.apache.struts.action.ActionServlet
action
*.do
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
"Content-Type" content="text/html; charset=UTF-8">
增加用户界面
package action;
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;
import model.DBHelperL;
public class AddAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
String action = request.getParameter("action");
if ("submit".equals(action)) {
if (name == null || name.length() <= 0) {
} else {
// 数据库增加操作
DBHelperL db = new DBHelperL();
db.add(name, sex, phone, address);
db.close();
// 重定向,防止刷新时重复提交表单!
response.sendRedirect("show.do");
}
}
return mapping.getInputForward();
}
}
public void add(String name, String sex, String phone, String address) throws SQLException {
String sql = "INSERT INTO users (name, sex, phone, address) VALUES(?,?,?,?)";
if (pstm == null) {
pstm = conn.prepareStatement(sql);
}
pstm.setString(1, name);
pstm.setString(2, sex);
pstm.setString(3, phone);
pstm.setString(4, address);
pstm.execute();
}
在
中,添加action。
"/add" type="action.AddAction" input="/pages/add.jsp">
"success" path="/show.do">
<%
DBHelperL db = new DBHelperL();
List> lists = db.queryAll();
db.close();
request.setAttribute("lists", lists);
%>
"add.do">增加用户
public List queryByID(String ID) throws SQLException {
List list = new ArrayList<>();
String sql = "SELECT * FROM users WHERE id = ?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, ID);
ResultSet rs = pstm.executeQuery();
while (rs.next()) {
// 将结果顺序存放到一个list中,作为结果返回
list.add(rs.getString(2));
list.add(rs.getString(3));
list.add(rs.getString(4));
list.add(rs.getString(5));
}
return list;
}
"/show" type="action.DeleteAction" input="/pages/show.jsp"
forward="/pages/show.jsp">
package action;
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;
import model.DBHelperL;
public class DeleteAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String ID = request.getParameter("deleteID");
DBHelperL db = new DBHelperL();
db.deleteByID(ID);
db.close();
return mapping.getInputForward();
}
}
public boolean deleteByID(String ID) throws SQLException {
String sql = "DELETE FROM users WHERE id = ?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, ID);
return pstm.execute();
}
name和sex不能修改,根据ID更新数据。
package action;
import java.util.List;
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;
import model.DBHelperL;
public class UpdateAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String ID = request.getParameter("updateID");
String action = request.getParameter("action");
DBHelperL db = new DBHelperL();
List list = db.queryByID(ID);
if (!list.isEmpty()) {
request.setAttribute("updateID", ID);
request.setAttribute("name", list.get(0));
request.setAttribute("sex", list.get(1));
request.setAttribute("phone_old", list.get(2));
request.setAttribute("address_old", list.get(3));
}
if ("submit".equals(action)) {
if (ID != null && ID.length() > 0) {
String phone_new = request.getParameter("phone_new");
String address_new = request.getParameter("address_new");
db.updateByID(ID, phone_new, address_new);
db.close();
response.sendRedirect("show.do");
}
}
db.close();
return mapping.getInputForward();
}
}
"/update" type="action.UpdateAction" input="/pages/update.jsp">
"success" path="/show.do">
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
String encoding = null;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
if (encoding == null || encoding.length() <= 0) {
encoding = "UTF-8";
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
Set Character Encoding
filters.SetCharacterEncodingFilter
encoding
UTF-8
Set Character Encoding
/*
response.sendRedirect("show.do");
用button
替代submit
,在JavaScript中满足条件时,调用form.submit()
。
"button" value="add" onclick="validate(this.form);">
JavaScript代码:
在默认的jsp文件中,转发到指定的URL。
"show.do">
http://pan.baidu.com/s/1i5rX6fz