session集合<httpSession,String>
session集合<httpSession,String>
任務19
這段沒有講清楚,弄了一整天才搞懂。
login_do連結到(影片中沒有看到這段。
/admin/goosd_list 轉發至
/admin/goods_list.jsp
如果是web项目可以将jar包放在WEB-INF的lib文件夹下,web项目将会自动识别
使用el表达式获取项目名
${pageContext.request.contextPath}
jstl 使用
1 在网上下载jstl 1.1.2 桌面资料里面有
2 在lib中引入 jstl
3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
在我们的jsp 页面上加入以上代码即可使用
使用 jstl表达式 存值
<c:set var="userName" value="sasas" scope="request"></c:set>
${userName}
第一行,我们把一个变量名为 userName 值为 sasas 的元素放进request 域中 然后使用el表达式的方式取值
取值
<c:out value="${userName}"></c:out>
移除
<c:remove var="userName"/>
放到取值表达式前面我们就不能渠道userName 这个元素了
适应jstl表达式中的 if if-else 循环标签
<c:set var="age" value="2" scope="request"></c:set>
<c:if test="${age>18}">
<font color="green">你已经成年</font>
</c:if>
if-else
<c:set var="age" value="2" scope="request"></c:set>
<c:choose>
<c:when test="${age>18}">
<font color="green">你已经成年</font>
</c:when>
<c:otherwise>
<font color="red">
你还未成年
</font>
</c:otherwise>
</c:choose>
普通的循环
<c:forEach var="i" begin="1" end="10">
中国${i}<br>
</c:forEach>
遍历集合对象
<%
List<User> list=new ArrayList<>();
list.add(new User("中国1","123",70,"女",false));
list.add(new User("中国2","123",70,"女",false));
list.add(new User("中国3","123",70,"女",false));
list.add(new User("中国4","123",70,"女",false));
list.add(new User("中国5","123",70,"女",false));
request.setAttribute("list",list);
%>
<c:forEach items="${requestScope.list}" var="u">
${u.userName}<br>
</c:forEach>
我们先将list对象放进request 中
然后使用forEach遍历
注意这里的items 必须是用el 表达式取到的
对于 index.jsp 的修改 ---之前是加入没有用户登录就是现实 登录 注册两个链接 。 否则就是现实目前登录的用户
<c:choose>
<c:when test="${empty user }">
<a href="<%=request.getContextPath()%>/login.jsp">登录</a>
<a href="<%=request.getContextPath() %>/register.jsp">注册</a>
</c:when>
<c:otherwise>
当前登录用户为:${user.userName}
</c:otherwise>
</c:choose>
对于goods_list.jsp的修改---之前是将tr td 嵌入if-else java语句块中 现在是
<c:choose>
<c:when test="${empty user }">
<a href="<%=request.getContextPath()%>/login.jsp">登录</a>
<a href="<%=request.getContextPath() %>/register.jsp">注册</a>
</c:when>
<c:otherwise>
当前登录用户为:${user.userName}
</c:otherwise>
</c:choose>
序列化、持久化:将Goods对象转化为JSON对象
Goods goods=new Goods("香蕉","夏威夷香蕉",100,400);
String json=JSON.toJSONString(goods);
System.out.println(json);
反序列化:将JSON对象转化为Goods对象
String s="{\"des\":\"夏威夷香蕉\",\"inventory\":400,\"name\":\"香蕉\",\"price\":100}";
Goods goods=JSON.parseObject(s,Goods.class);
System.out.println(goods.getName()+" "+goods.getDes()+" "+goods.getInventory()
+" "+goods.getPrice());
用构造方法防止外部类实例化工具类
httpservlet 的生命周期 执行构造方法-->init方法-->doget方法-->service-->destory
web工程中的jar包直接放到web-inf下面的lib中。
客户端路径:需要浏览器解析,需要使用绝对路径使用<%=request.getcontentpath()%>获取当前路径
服务端路径:在服务器端解析,需要使用相对路径。
单例模式,主要用于节约资源提升性能,如:
package tools;
/**单例模式标准设计**/
public class JDBCUtil {
public static JDBCUtil instance=new JDBCUtil();
private JDBCUtil() {
/***构造方法私有化是为了只能通过instance来实例化工具对象,不能让外部类访问***/
}
public void getConnect() {
System.out.print("获取连接");
}
public static void main(String[] args) {
}
}
MVC分层框架实质上是对代码的分类,不同的类负责不同的功能,方便后期维护。
model 层是指模型,即数据模型,每个类实际上是一个模型
view层视图,只是负责页面展示(jsp)。
controller层是模型层(servlet),业务逻辑处理,比如查询数据,校验数据,用户注册.......
企业级java开发架构
WEB层 (JSP Servlet 层 , Controller层 )
Serverice 业务逻辑处理
Dao层 数据的增删改查
Dao层一般是按照数据模型来创建的,