插入数据实现
list.jsp 21行,跳转到另一个页面,接受输入 <p class="g_title fix">内容列表 <a class="btn03" href="index.jsp">新 增</a>
和list.jsp同级创建index.jsp,写一个表单
<form action="<%=basePath %>InsertOneServlet.action" id="mainForm" method="post">
<tr>
<td width="90" align="right">指令名称:</td>
<td>
<input name="command" type="text" class="allInput" value="${command}"/>
</td>
</tr>
<tr>
<td width="90" align="right">描述:</td>
<td>
<input name="description" type="text" class="allInput" value="${description}"/>
</td>
</tr>
<tr>
<td width="90" align="right">内容:</td>
<td>
<input name="content" type="text" class="allInput" value="${content}"/>
</td>
</tr>
<tr>
<td colspan="2" width="85" align="right"><input type="submit" class="tabSub" value="确定" /></td>
</tr>
创建InsertOneServlet,配置web.xml
//接受页面的值
String command = req.getParameter("command");
String description = req.getParameter("description");
String content = req.getParameter("content");
MaintainService maintainService = new MaintainService();
maintainService.insertOne(command, description, content);
//向页面跳转
req.getRequestDispatcher("/List.action").forward(req, resp);
编写service
public void insertOne(String command,String description,String content){
MessageDao messageDao = new MessageDao();
Message message = new Message();
message.setCommand(command);
message.setDescription(description);
message.setContent(content);
messageDao.insertOne(message);
}
编写DAO
方法头部 public void insertOne(Message m)
sqlSession = dBAccess.getSqlSession();
//通过sqlSession实现sql语句
sqlSession.insert("Message.insertOne",m);
System.out.println("插入方法执行....."+"content:"+m.getContent());
sqlSession.commit();
配置Message.xml
<insert id="insertOne" parameterType="com.imooc.bean.Message" useGeneratedKeys="false">
INSERT INTO MESSAGE
( ID,
COMMAND,
DESCRIPTION,
CONTENT
)
VALUES
( #{id},
#{command,jdbcType=VARCHAR},
#{description,jdbcType=VARCHAR},
#{content,jdbcType=BOOLEAN}
)
</insert>
我先从Message.xml开始写,写到service不知道方法的参数该怎么写,又从页面开始写,用三个String接收输入,在service中传给Message对象