struts2编写表单提交简单的(2)
实体
package com.oak.entity;
public class User {
private int id;
private String username;
private String password;
private int sex;
private int age;
private double salary;
private String address;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", sex=" + sex + ", age=" + age + ", salary=" + salary
+ ", address=" + address + ", phone=" + phone + "]";
}
}
dao层
package com.oak.dao;
import java.util.List;
import com.oak.entity.User;
public interface UserDao {
//登录
public User login(String username,String password);
//列表
public List<User> findAll();
//添加用户数据
public void userAdd(User user);
//删除用户
public User userdelete(int id);
//进入修改页面(回显)
public User usertoupdate(User user);
//用户修改
public void userupdate(User user);
}
daoimpl层
package com.oak.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.oak.dao.UserDao;
import com.oak.entity.User;
import com.oak.utils.DBUtil;
public class UserDaoImpl implements UserDao {
private Connection connection=null;
private PreparedStatement p = null;
private ResultSet set = null;
@Override
public User login(String username, String password) {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "select * from t_user where username = ? and password = ?";
p = connection.prepareStatement(sql);
p.setString(1, username);
p.setString(2, password);
set = p.executeQuery();
User user = new User();
while(set.next()){
user.setId(set.getInt("id"));
user.setUsername(set.getString("username"));
user.setPassword(set.getString("password"));
user.setSex(set.getInt("sex"));
user.setAge(set.getInt("age"));
user.setSalary(set.getDouble("salary"));
user.setAddress(set.getString("address"));
user.setPhone(set.getString("phone"));
}
return user;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public List<User> findAll() {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "select * from T_USER";
p = connection.prepareStatement(sql);
set = p.executeQuery();
List<User> list = new ArrayList<User>();
while(set.next()){
User user = new User();
user.setId(set.getInt("id"));
user.setUsername(set.getString("username"));
user.setPassword(set.getString("password"));
user.setSex(set.getInt("sex"));
user.setAge(set.getInt("age"));
user.setSalary(set.getDouble("salary"));
user.setAddress(set.getString("address"));
user.setPhone(set.getString("phone"));
list.add(user);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void userAdd(User user) {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "insert into T_USER values(T_USER_seq.nextval,?,?,?,?,?,?,?)";
p = connection.prepareStatement(sql);
p.setString(1, user.getUsername());
p.setString(2, user.getPassword());
p.setInt(3, user.getSex());
p.setInt(4, user.getAge());
p.setDouble(5, user.getSalary());
p.setString(6, user.getAddress());
p.setString(7, user.getPhone() );
p.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public User userdelete(int id) {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "delete from T_USER where id=?";
p = connection.prepareStatement(sql);
p.setInt(1, id);
p.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public User usertoupdate(User user) {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "select * from T_USER where id = ?";
p = connection.prepareStatement(sql);
p.setInt(1, user.getId());
set = p.executeQuery();
if(set.next()){
user.setId(set.getInt("id"));
user.setUsername(set.getString("username"));
user.setPassword(set.getString("password"));
user.setSex(set.getInt("sex"));
user.setAge(set.getInt("age"));
user.setSalary(set.getDouble("salary"));
user.setAddress(set.getString("address"));
user.setPhone(set.getString("phone"));
}
return user;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void userupdate(User user) {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "update T_USER set username=?,password=?,sex=?,age=?,salary=?,address=?,phone=? where id=?";
p = connection.prepareStatement(sql);
p.setString(1, user.getUsername());
p.setString(2, user.getPassword());
p.setInt(3, user.getSex());
p.setInt(4, user.getAge());
p.setDouble(5, user.getSalary());
p.setString(6, user.getAddress());
p.setString(7, user.getPhone());
p.setInt(8, user.getId());
p.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
servlet
package com.oak.service;
import java.util.List;
import com.oak.entity.User;
public interface UserService {
//登录
public User login(String username, String password);
//列表
public List<User> finfAll();
//添加用户数据
public void userAdd(User user);
//删除用户
public User userdelete(int id);
//进入修改页面
public User usertoupdate(User user);
//用户修改
public void userupdate(User user);
}
servletimpl层
package com.oak.service.impl;
import java.util.List;
import com.oak.dao.UserDao;
import com.oak.dao.impl.UserDaoImpl;
import com.oak.entity.User;
import com.oak.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl();
@Override
public User login(String username, String password) {
// TODO Auto-generated method stub
return userDao.login(username, password);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<User> finfAll() {
// TODO Auto-generated method stub
return userDao.findAll();
}
@Override
public void userAdd(User user) {
// TODO Auto-generated method stub
userDao.userAdd(user);
}
@Override
public User userdelete(int id) {
// TODO Auto-generated method stub
userDao.userdelete(id);
return userDao.userdelete(id);
}
@Override
public User usertoupdate(User user) {
// TODO Auto-generated method stub
return userDao.usertoupdate(user);
}
@Override
public void userupdate(User user) {
// TODO Auto-generated method stub
userDao.userupdate(user);
}
}
action
package com.oak.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.oak.entity.User;
import com.oak.service.UserService;
import com.oak.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private User user;
private User u;
private User uu;
private String cod;
private List<User> userlist;
private UserService userService = new UserServiceImpl();
//登录
public String login(){
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("username", user.getUsername());
HttpSession session = request.getSession();
session.setAttribute("user", user.getUsername());
u = userService.login(
user.getUsername(),
user.getPassword());
if(u.getUsername() != null){
return SUCCESS;
}else {
return ERROR;
}
}
//表单
public String list(){
userlist=userService.finfAll();
return SUCCESS;
}
//增加
public String userAdd(){
userService.userAdd(user);
return SUCCESS;
}
//删除
public String userdelete(){
uu = userService.userdelete(user.getId());
return SUCCESS;
}
//进入修改页面
public String usertoupdate(){
userService.usertoupdate(user);
return SUCCESS;
}
//修改
public String userupdate(){
userService.userupdate(user);
return SUCCESS;
}
//全局变量getset方法
}
struts.xml
<package name="word" namespace="/" extends="struts-default">
<default-action-ref name="errorAction"></default-action-ref>
<action name="logindemo" class="com.oak.action.UserAction" method="login">
<result name="success">
/admin_index.jsp
</result>
<result name="error">
/login.jsp
</result>
</action>
<!-- 用户管理 -->
<action name="user_list" class="com.oak.action.UserAction" method="list">
<result name="success">
/userAdmin.jsp
</result>
</action>
<!-- 用户添加 -->
<action name="user_add" class="com.oak.action.UserAction" method="userAdd">
<result name="success" type="redirectAction">
user_list
</result>
</action>
<!-- 用户删除 -->
<action name="user_delete" class="com.oak.action.UserAction" method="userdelete">
<result name="success" type="redirectAction">
user_list
</result>
</action>
<!-- 用户修改页面 -->
<action name="user_toupdate" class="com.oak.action.UserAction" method="usertoupdate">
<result name="success">
/toupdate.jsp
</result>
</action>
<!-- 用户修改 -->
<action name="user_update" class="com.oak.action.UserAction" method="userupdate">
<result name="success" type="redirectAction">
user_list
</result>
</action>
<!-- 姓名查询 -->
<action name="user_read" class="com.oak.action.UserAction" method="usertoupdate">
<result name="success" >
/usernametoupdate.jsp
</result>
</action>
</package>
struts2编写表单提交简单的(2)的更多相关文章
- php框架中的phalcon框架的安装,及初步认识,从表单提交简单的数据到数据库中
php框架中的phalcon框架的安装,及初步认识,从表单提交简单的数据到数据库中 1.phalcon框架的安装: phalcon框架在windows本地安装可以利用wamp软件,安装之后可以查看对应 ...
- struts2 jsp表单提交后保留表单中输入框中的值 下拉框select与input
原文地址:struts2 jsp表单提交后保留表单中输入框中的值 下拉框select与input jsp页面 1 function dosearch() {2 if ($(&q ...
- HTML5:表单提交
不加CSS.JavaScrips的HTML表单提交简单代码 <!DOCTYPE html> <html lang="en"> <head> &l ...
- JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传
Struts2核心流程图 1. Struts2 和 Struts1 对比 struts1:基于Servlet(ActionServlet),actionForm众多(类的爆炸),action单例(数据 ...
- Maven web项目(简单的表单提交) 搭建(eclipse)
我们将会搭建一个,基于Maven管理的,具有简单的表单提交功能的web项目,使用DAO--service--WEB三层结构,服务器使用Tomcat 1 项目基本结构的搭建 左上角File---> ...
- Ajax表单提交及后台处理简单应用
首先先说下表单提交吧,要提交表单那么就得先收集表单数据(至于验证这个我就不说了,要说留下下次吧),有了jquery取个html的值还是简单$("xxid").val()等就完了,但 ...
- Struts2 03---数据封装+获取表单提交数据
Struts的数据封装分为三种:属性封装,模型驱动,表达式封装.下面以获取表单提交数据来简单介绍一下Struts的数据封装. <form action="loginlogin. ...
- java post请求的表单提交和json提交简单小结
在java实现http请求时有分为多种参数的传递方式,以下给出通过form表单提交和json提交的参数传递方式: public String POST_FORM(String url, Map< ...
- PHP -- 简单表单提交
网上看博文,一步步入门~~ 简单表单,简单提交 @_@!! <?php //php代码部分开始 echo "<html>"; echo "<hea ...
随机推荐
- chrome 监听touch类事件报错:无法被动侦听事件preventDefault
先上错误信息: Unable to preventDefault inside passive event listener due to target being treated as passiv ...
- 利用js对象将iframe数据缓存, 实现子页面跳转后, 返回时不丢失之前填写的数据
利用js对象将iframe数据缓存, 实现子页面跳转后, 返回时不丢失之前填写的数据 实现描述:将数据存放在js对象中, 然后放在父页面的document对象中, 在页面刷新的时候将父页面的值取出来, ...
- c# 金钱大写转小写工具类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Linux系列(17)之系统服务
我们知道,在我们登陆Linux后,系统就为我们提供了很多服务,比如例行工作调度服务crond.打印服务.邮件服务等.那么这些服务是如何被启动的呢? 这个问题先放一下,接下来我们先了解一下Linux的启 ...
- ROS的初步学习--创建一个工作空间和一个程序包
快速开始 创建工作区(workspace) 工作区可以作为一个独立的项目进行编译,存放ROS程序的源文件.编译文件和执行文件.建立工作区的方法如下: mkdir -p ~/catkin_ws/src ...
- 怎样创建一个独立于当前文档的新的Document对象
使用: document.implementation. 如下所示, 新创建的Document对象可以正常使用相关属性和方法, 然后将它的根节点与当前文档的根节点做一个替换. var doc = do ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction 问题解决
有两种设置方法 第一种在mysql的配置文件中加入,然后重启mysql innodb_lock_wait_timeout = 500 第二种直接执行如下命令 set global innodb_loc ...
- 二、LinkList及其源码解析
1.链表介绍 链表是一种物理单元上非连续,非顺序的存储结构.链表由一系列的姐点组成,结点可以在运行时动态生成.每个结点包含两个部分,一个是存储数据元素的数据域,一个是存储下一个结点的指针域 双链表是链 ...
- R 读取xls/xlsx文件
包readxl install.packages('readxl',repois='https://mirrors.utsc.edu.cn/CRAN/) library(readxl) # read_ ...
- 文件的空间使用和IO统计
数据库占用的存储空间,从高层次来看,可以查看数据库文件(数据文件,日志文件)占用的存储空间,从较细的粒度上来看,分为数据表,索引,分区占用的存储空间.监控数据库对象占用的硬盘空间,包括已分配,未分配, ...