Struts框架核心技术小小班
? Web 开发基础
–B/S结构程序
–请求响应机制
–HTML
–Servlet
–JSP
–Web 服务器
? 编程思想
–分层开发思想
–面向接口编程思想
? 设计模式
–DAO设计模式
–MVC设计模式
–因为Struts是Servlet+JSP的MVC解决方案,所以在学习Struts之前有必要了解如下Web开发的基础知识,使用Servlet+JSP如何编程,以及编程思想和设计模式中Web开发中应用
? 这对我们在学习Struts时是很有帮助的
? 我们要比较Servlet+JSP程序的MVC模式实现和Struts MVC的异同
? Servlet+JSP程序的弊端
? Struts的优势
? 程序开发的思想
? 设计模式的应用
– B/S结构程序
? B/S结果程序是指,基于Browser(浏览器)/Server(服务器)的应用程序
? 相对C/S结构程序而言,B/S结构程序有如下优点:
– 基于网络
– 共享性好
– 客户端零维护
– 请求响应机制
? B/S程序的主要特征是请求(request)响应(response)机制
? 通常使用的协议是Http协议,该协议是无状态的
– HTML
? HTML的全称是Hyper Text Markup Language(超文本标记语言)
?基本结构是:
<HTML>
<HEAD>
<title>, <base>, <link>, <meta>
</HEAD>
<BODY>
HTML 文件的正文写在这里... ...
</BODY>
</HTML>
– Servlet
? Servlet 是为动态创建Web工程而提供的编程接口
MyServlet.java
? 为了解决Servlet的开发效率问题而设计的开发语言
? 底层执行还是Servlet
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class MyJsp_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write('\r');
out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" <title>My JSP 'MyJsp.jsp' starting page</title>\r\n");
out.write(" \r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write("\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" This is my JSP page. <br>\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
– Web 服务器
? Web工程的运行容器,如Tomcat
–分层开发思想
? 软件的层次结构可以分为四层:
–表现层
–控制层
–业务逻辑层
–数据逻辑层(持久层)
–面向接口编程思想
? 在编程中将业务逻辑抽象出接口,以供上次调用
? 依赖抽象(接口),而非具体(接口实现)的编程思想,又称之为控制反转(Inversion of Control)
–DAO设计模式
? DAO设计模式
–DAO的全称是:Data Access Object,数据访问对象。
–使用DAO设计模式,来封装数据库持久层的所有操作(CRUD),使低级的数据逻辑和高级的业务逻辑分离,达到解耦合的目的。
? 一个典型的DAO实现有如下的组件:
–一个 DAO 接口
–一个实现了 DAO 接口的具体类
–一个 DAO 工厂类
–数据传输对象(有时称为值对象)
?以维护一个客户信息为例,具体组件如下所示:
–CustomerDao 接口
–Customer 值对象(VO)
–CustomerDaoImpl(接口的具体实现类)
–CustomerFactory(工厂类,实例化用)
? MVC的全称是:Model(模型) View(视图) Controller( 控制器)
? Model
–对应业务逻辑层、数据逻辑层
–由接口及其实现类充当
? View
–对应表现层
–由HTML页面、JSP页面、Tag(标签)等充当
? Controller
–对应控制层
–由Servlet 或Struts中的Action等充当
–图示
–以添加客户和查询客户列表为例来说明上述内容
–程序运行结果
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.redking.dao.CustomerDao;
import com.redking.util.ConnectionUtil;
import com.redking.util.SQLConstants;
import com.redking.vo.Customer;
public class CustomerDaoImpl implements CustomerDao,SQLConstants{
public List listCustomer() {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = cu.getConnection();
//conn.setAutoCommit(false);设置默认不自动提交
//Statement stmt;静态添加
//PrepareStatement pstmt;动态添加
List list = new ArrayList();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY_CUSTOMER_SQL);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
String email = rs.getString(3);
Customer c = new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
list.add(c);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
public void save(Customer c) {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = cu.getConnection();
//conn.setAutoCommit(false);设置默认不自动提交
//Statement stmt;静态添加
//PrepareStatement pstmt;动态添加
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(ADD_CUSTOMER_SQL);
pstmt.setString(1, c.getName());
pstmt.setString(2, c.getEmail());
pstmt.executeUpdate();
//conn.commit();设置强制提交
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.redking.dao.CustomerDao;
import com.redking.dao.impl.CustomerDaoImpl;
import com.redking.vo.Customer;
public class CustomerServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public CustomerServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
//响应用户请求
String name = request.getParameter("name");
String email = request.getParameter("email");
//调用后台逻辑-dao
CustomerDao dao = new CustomerDaoImpl();
Customer c = new Customer();
c.setName(name);
c.setEmail(email);
dao.save(c);
//跳转其他页面
//RequestDispatcher
request.getRequestDispatcher("/pages/Customer.jsp").forward(request,response);
*/
String methodName = request.getParameter("methodName");
if(methodName!=null&&methodName.equals("save")){
save(request,response);
}else{
list(request,response);
}
}
public void save(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求
String name = request.getParameter("name");
String email = request.getParameter("email");
//调用后台逻辑-dao
CustomerDao dao = new CustomerDaoImpl();
Customer c = new Customer();
c.setName(name);
c.setEmail(email);
dao.save(c);
//跳转其他页面
//RequestDispatcher
//request.getRequestDispatcher("/pages/Customer.jsp").forward(request,response);
list(request,response);
}
public void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求
String name = request.getParameter("name");
String email = request.getParameter("email");
//调用后台逻辑-dao
CustomerDao dao = new CustomerDaoImpl();
List list = dao.listCustomer();
request.setAttribute("CustomerList", list);
//跳转其他页面
//RequestDispatcher
request.getRequestDispatcher("/pages/Customer.jsp").forward(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="redking" %>
<%
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 'Customer.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="f1" id="f1" action="<%=path %>/servlet/CustomerServlet?methodName=save" method="post">
<table border="0">
<tr>
<td>Name:</td>
<td><input type="text" name="name" ></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" ></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="保存"></td>
</tr>
</table>
</form>
<hr>
<table>
<tr><th>ID</th><th>Name</th><th>Email</th></tr>
<%--
<%
List list = (List) request.getAttribute("CustomerList");
//遍列LIST
for(int i=0;i<list.size();i++){
Customer c = (Customer) list.get(i);
out.println("<tr>");
out.println("<td>");
out.println(c.getId());
out.println("</td>");
out.println("<td>");
out.println(c.getName());
out.println("</td>");
out.println("<td>");
out.println(c.getEmail());
out.println("</td>");
out.println("</tr>");
}
%>
--%>
<%--使用标准标签 --%>
<redking:forEach var="c" items="${CustomerList}">
<tr>
<td>
${c.id }
</td>
<td>
${c.name }
</td>
<td>
${c.email }
</td>
</tr>
</redking:forEach>
</table>
</body>
</html>
Struts框架核心技术小小班相关推荐
- 深入浅出学习Struts框架(九):分析Struts框架实例4
前两篇博客介绍了ActionServlet的初始化,讲述了它在初始化的时候做的事情.有了ActionServlet初始化,才能说当我们访问页面如何利用digester读取struts-config.x ...
- 基于Tiles框架Struts框架的UI 设计—几种组织HTML和JSP视图组件
摘要每个web应用开发者一定组织视图组件,例如 页首,正文体和页尾. 有许多技术可以组织这些组件, 但每个都有优缺点.这篇文章包括了7个可用的解决方案并让你了解 Tiles框架和Struts框架的灵 ...
- ssh框架怎么写ajax,SSH框架+Ajax(运用dwr框架)综合小实例[精校版本]
<SSH框架+Ajax(运用dwr框架)综合小实例[精校版本]>由会员分享,可在线阅读,更多相关<SSH框架+Ajax(运用dwr框架)综合小实例[精校版本](36页珍藏版)> ...
- struts(三)——struts框架实现登录示例
前两篇讲解了struts框架的基本实现,自己感觉讲的也有些枯燥,今天拿登录的例子来做一个实现. 1.新建一个javaweb项目,并将struts的jar包拷贝到WebRoot/WEB-INF/lib下 ...
- Struts框架的入门使用
1.struts框架的使用 导入jar包 1.commons-fileupload-1.2.jar 2. freemarker-2.3.15.jar 3.ognl-2.7.3.jar 4.struts ...
- java 详解 搭建 框架_在Eclipse中搭建Struts框架过程详解
虽然用MyEclipse搭建Struts框架是更为便捷的方式,但是用Eclipse可以增强自己对Struts的理解.本文演示了使用Eclipse搭建Struts 1.2框架的过程.此项目实现了简单的功 ...
- c#Struts框架理念和自制Struts框架下 复杂版
在上文中,我提到了一个"简单的zjy框架"的数据流程,但是那只是很少的一部分,用法和功能也比较简单,他能够完成的只是把一个数据库显示到页面(或者窗体)上.在这个第四章节中,我想将这 ...
- mysql国际化_Java的Struts框架中的主题模板和国际化设置
主题模板 如果不指定一个主题,然后Struts2中会使用默认的XHTML主题.例如Struts 2中选择标签: 生成HTML标记: Name: 这里empinfo struts.xml文件中定义动作名 ...
- .NET框架之“小马过河”
.NET框架之"小马过河" 有许多流行的 .NET框架,大家都觉得挺"重",认为很麻烦,重量级,不如其它"轻量级"框架,从而不愿意使用.面对 ...
最新文章
- matlab中nchoosek函数的用法
- 查看服务器硬盘负载——判断硬盘是否为瓶颈
- 狂宴终有尽时,留一份清醒一份醉 比特币现金BCH凸显投资价值
- Spring MVC - 配置Spring MVC
- idea创建包怎么让包分层_开一家早餐店卖生煎包怎么样
- 如何在Angular单元测试里,对class protected方法进行测试
- 查看tensorflow版本以及路径:
- java.exe 不是有效的win32_WinXP提示不是有效的Win32应用程序怎么办?
- SPSS教程-t检验怎么做?
- 服务器上Dll文件读取失败,解决开机出现dll文件加载失败的方法
- 国企“造船”转行测试,成功拿下11K,如今谁又甘心平庸呢?
- Xposed Installer框架v3.1.5 安卓版
- c语言中e的n次方怎么打,C语言中N次方怎么打
- 【转】 笔记本散热维护
- PDF Expert永久版
- 初来乍到,余生请多指教
- (十六)Hibernate中的延迟加载
- java实现md5签名算法
- 网狐荣耀斗地主等15合1(美女图)
- 利用kmplayer查看wav声音文件的采样率
热门文章
- 数据建模学习笔记-2-《高质量数据库建模 2-建模流程》
- 自制精排 ePub 集、不定期更新(UPDATA-2015-8-2)
- Servlet学习-request
- CF#190DIV.1
- 【财经】创业的10条定律 10大烦恼
- LaTeX入门第三集!LaTeX的几个应用!
- anaconda安装scrapy失败的解决方法(2020.7.7)
- linux smart服务,RAKsmart Linux美国服务器常用信息命令
- c语言用法 我说火罐火车 刘华火车,五塘村社区建“火车头广场”
- python编写函数、计算三个数的最大公约数_python 函数求两个数的最大公约数和最小公倍数...