JSP表单
- method中的get和post的区别(默认为get)
- get地址明参,post将参数隐藏
- get不支持正确的中文转码,post才能够正确的转码
- get对应调用doget,post对应调用dopost
- 从浏览器手动输入参数是get方式
增改
- 新增
- 在dao中添加hibernate的add方法调用session的save方法,需要传对象
- 再将方法暴露给dao接口
- 在service中添加add方法调用 daoI的add方法,需要传对象
- 再将方法暴露给serviceI
- 在action中调用service的add方法
-
全盘复制新增为修改,修改对应的方法名,action调用名,增加action通过方法名
-
复制查询所有方法 修改为 按主键查询方法,需要注意的是在action中,将返回的
对象存到request中 -
在update页面调用按主键查询action,正确使用表达式传参
-
在list页面循环中添加修改的超链接,超链接传参的格式符合update页面需要的格式
- struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<include file="struts-Serviceman.xml"></include>
<package name="struts2" extends="struts-default" namespace="/">
</package>
</struts>
- struts-Serviceman.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="struts-Serviceman" extends="struts2" namespace="/">
<action name="ServicemanAction_*" class="repairsystem.action.ServicemanAction" method="{1}">
<allowed-methods>getServicemanInfo,addServiceman,updateServiceman,getServicemanInfoBySid</allowed-methods>
</action>
</package>
</struts>
- ServicemanDao.java
package repairsystem.dao.imp;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import repairsystem.dao.ServicemanDaoI;
import repairsystem.entity.Serviceman;
import repairsystem.factory.HibernateSessionFactory;
public class ServicemanDao implements ServicemanDaoI{
private Session session = HibernateSessionFactory.getSession();
private Transaction tran;
public List<Serviceman> getServicemanInfo() {
Query query = session.createQuery("From Serviceman");
List<Serviceman> servicemans = query.list();
HibernateSessionFactory.closeSession();
return servicemans;
}
public Serializable addServiceman(Serviceman serviceman){
tran = session.beginTransaction();
Serializable result = session.save(serviceman);
tran.commit();
HibernateSessionFactory.closeSession();
return result;
}
public Serializable updateServiceman(Serviceman serviceman){
tran = session.beginTransaction();
session.update(serviceman);
tran.commit();
HibernateSessionFactory.closeSession();
return 0;
}
public Serviceman getServicemanInfoBySid(Integer sid){
Serviceman serviceman = session.load(Serviceman.class,sid);
return serviceman;
}
}
- ServicemanDaoI.java
public interface ServicemanDaoI {
public List<Serviceman> getServicemanInfo();
public Serializable addServiceman(Serviceman serviceman);
public Serializable updateServiceman(Serviceman serviceman);
public Serviceman getServicemanInfoBySid(Integer sid);
}
- ServicemanService.java
public Serializable addServiceman(Serviceman serviceman){
return servicemanDaoI.addServiceman(serviceman);
}
public Serializable updateServiceman(Serviceman serviceman){
return servicemanDaoI.updateServiceman(serviceman);
}
public Serviceman getServicemanInfoBySid(Integer sid){
return servicemanDaoI.getServicemanInfoBySid(sid);
}
- ServicemanServiceI.java
public interface ServicemanServiceI {
public List<Serviceman> getServicemanInfo();
public Serializable addServiceman(Serviceman serviceman);
public Serializable updateServiceman(Serviceman serviceman);
public Serviceman getServicemanInfoBySid(Integer sid);
}
- ServicemanAction.java
// 根据主键查询维修人员
public String getServicemanInfoBySid() throws Exception {
Serviceman dbserviceman = servicemanServiceI.getServicemanInfoBySid(serviceman.getSid());
ServletActionContext.getRequest().setAttribute("serviceman", dbserviceman);
// System.out.println(dbteacher.getTname());
return null;
}
// 增加维修人员
public String addServiceman() {
Serializable result = servicemanServiceI.addServiceman(serviceman);
return null;
}
// 修改维修人员信息
public String updateServiceman() {
Serializable result = servicemanServiceI.updateServiceman(serviceman);
return null;
}
- add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ADD</title>
<style type="text/css">
.content{width:680px;margin-left: auto;margin-right: auto;}
.content form div > span:nth-child(1){width:140px;display: inline-block;}
.content form div > span > input{width:530px;height:30px;margin-top:10px;}
#add{width:60px; height:36px;margin-top:10px;margin-left:540px;}
</style>
</head>
<body>
<div class="content">
<form action="ServicemanAction_addServiceman.action" method="post">
<div><span>维修者编号:</span><span><input type="text" name="serviceman.sid"></span></div>
<div><span>维修者姓名:</span><span><input type="text" name="serviceman.snam"></span></div>
<div><span>联系电话:</span><span><input type="text" name="serviceman.sphone"></span></div>
<div><span>相片:</span><span><input type="text" name="serviceman.simg"></span></div>
<div><span>密码:</span><span><input type="text" name="serviceman.spassword"></span></div>
<div><span>地址:</span><span><input type="text" name="serviceman.saddress"></span></div>
<div><span>状态:</span><span><input type="text" name="serviceman.sstatus"></span></div>
<div><input id="add" type="submit" value="增加"></div>
</form>
</div>
</body>
</html>
- update.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>UPDATE</title>
<style type="text/css">
.content{width:680px;margin-left: auto;margin-right: auto;}
.content form div > span:nth-child(1){width:140px;display: inline-block;}
.content form div > span > input{width:530px;height:30px;margin-top:10px;}
#update{width:60px; height:36px;margin-top:10px;margin-left:540px;}
</style>
</head>
<body>
<s:action name="ServicemanAction_getServicemanInfoBySid" executeResult="false"
namespace="/">
<s:param name="serviceman.sid">
<s:property value="#parameters.sid" />
</s:param>
</s:action>
<div class="content">
<form action="ServicemanAction_updateServiceman.action" method="post">
<div><span>维修者编号:</span><span><input type="text" name="serviceman.sid" value="${serviceman.sid}"></span></div>
<div><span>维修者姓名:</span><span><input type="text" name="serviceman.snam" value="${serviceman.snam}"></span></div>
<div><span>联系电话:</span><span><input type="text" name="serviceman.sphone" value="${serviceman.sphone}"></span></div>
<div><span>相片:</span><span><input type="text" name="serviceman.simg" value="${serviceman.simg}"></span></div>
<div><span>密码:</span><span><input type="text" name="serviceman.spassword" value="${serviceman.spassword}"></span></div>
<div><span>地址:</span><span><input type="text" name="serviceman.saddress" value="${serviceman.saddress}"></span></div>
<div><span>状态:</span><span><input type="text" name="serviceman.sstatus" value="${serviceman.sstatus}"></span></div>
<div><input id="update" type="submit" value="修改"></div>
</form>
</div>
</body>
</html>
- serviceman.jsp
<table border="1">
<tr>
<td>维修者id</td>
<td>姓名</td>
<td>电话</td>
<td>密码</td>
<td>住址</td>
<td>状态</td>
<td>操作</td>
</tr>
<s:iterator var="serviceman" value="#request.servicemans">
<tr>
<td><s:property value="#serviceman.sid" /></td>
<td><s:property value="#serviceman.snam" /></td>
<td><s:property value="#serviceman.sphone" /></td>
<td><s:property value="#serviceman.spassword" /></td>
<td><s:property value="#serviceman.saddress" /></td>
<td><s:property value="#serviceman.sstatus" /></td>
<td><a href="Serviceman/update.jsp?sid=${serviceman.sid}">修改</a></td>
</tr>
</s:iterator>
<tr>
<td><a href="Serviceman/add.jsp">增加</a></td>
</tr>
</table>
新增add编码流程.png
网友评论