一、创建控制层
(1)创建VO值对象,对应页面表单的属性值
package com.cppdy.ssh.web.form; /** * VO值对象,对应页面表单的属性值 * VO对象与PO对象的关系: * 相同点:都是javabean * 不同点:PO对象中的属性关联数据库的字段 * VO对象中的属性可以随意增加、修改、删除,对应的页面表单属性 * */ @SuppressWarnings("serial") public class ElecTextForm implements java.io.Serializable { private String textID; private String textName; private String textDate; private String textRemark; public String getTextID() { return textID; } public void setTextID(String textID) { this.textID = textID; } public String getTextName() { return textName; } public void setTextName(String textName) { this.textName = textName; } public String getTextDate() { return textDate; } public void setTextDate(String textDate) { this.textDate = textDate; } public String getTextRemark() { return textRemark; } public void setTextRemark(String textRemark) { this.textRemark = textRemark; } }
(2)创建action类,继承ActionSupport类,实现驱动类ModelDriven
package com.cppdy.ssh.web.action; import com.cppdy.ssh.web.form.ElecTextForm; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @SuppressWarnings("serial") public class ElecTextAction extends ActionSupport implements ModelDriven<ElecTextForm>{ private ElecTextForm elecTextForm = new ElecTextForm(); public ElecTextForm getModel() { return elecTextForm; } public String save(){ System.out.println(elecTextForm.getTextName()); return "save"; } }
二、创建struts.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 配置action的访问路径为.do的形式 --> <constant name="struts.action.extension" value="do"></constant> <!-- 配置struts的开发模式 --> <constant name="struts.devMode" value="true"></constant> <!-- 配置struts的简单模式 --> <constant name="struts.ui.theme" value="simple"></constant> <package name="system" namespace="/system" extends="struts-default"> <action name="elecTextAction_*" class="com.cppdy.ssh.web.action.ElecTextAction" method="{1}"> <result name="save"> /system/textAdd.jsp </result> </action> </package> </struts>
三、添加struts过滤器(web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
四、添加jsp页面
(1)index.jsp添加连接地址
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <a href="<%=basePath%>system/textAdd.jsp">测试添加功能</a> </body> </html>
(2)添加功能测试页面
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>测试专用jsp</title> <script language="javascript"> function checkchar(){ document.Form1.action="elecTextAction_save.do"; document.Form1.submit(); alert(" 保存成功!"); } function addEnter(element){ document.getElementById(element).value = document.getElementById(element).value+"<br>"; } </script> </head> <body> <form name="Form1" id="Form1" method=post> <table cellspacing="1" cellpadding="5" width="90%" align="center" bgcolor="#f5fafe" style="border:1px solid #8ba7e3" border="0"> <tr> <td class="ta_01" colspan=2 align="center" background="${pageContext.request.contextPath }/images/b-info.gif"> <font face="宋体" size="2"> <strong>测试专用jsp</strong> </font> </td> </tr> <TR height=10><td></td><td></td></TR> <tr> <td class="ta_01" align="center" bgcolor="#f5fafe" width="15%">测试名称:</td> <td class="ta_01" bgcolor="#ffffff" style="word-break: break-all"> <textarea name="textName" id="textName" style=" 500px; height: 160px; padding: 1;FONT-FAMILY: 宋体; FONT-SIZE: 9pt" onkeydown="if(event.keyCode==13)addEnter('textName');"></textarea> </td> </tr> <tr> <td class="ta_01" style=" 100%" align="center" bgcolor="#f5fafe" colspan="2"> <input type="button" name="BT_Submit" value="保存" onclick="checkchar()" id="BT_Submit" style="font-size:12px; color:black; height=20;width=50"> </td> </tr> </table> </form> </body> </html>
五、发布web项目
六、启动项目
七:访问项目(http://localhost:8080/ssh/)
八、添加数据,点击保存
九、查看控制台打印jsp添加的数据
十、项目结构