一、JSP开发模式
1、JSP+JavaBean——计算器
- CalculatorBean.java:
package cn.itcast; import java.math.BigDecimal; public class CalculatorBean { private double firstNum; private double secondNum; private char operator = '+'; private double result; public double getFirstNum() { return firstNum; } public void setFirstNum(double firstNum) { this.firstNum = firstNum; } public double getSecondNum() { return secondNum; } public void setSecondNum(double secondNum) { this.secondNum = secondNum; } public char getOperator() { return operator; } public void setOperator(char operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public void calculate() { switch(this.operator) { case '+':{ this.result = this.firstNum + this.secondNum; break; } case '-':{ this.result = this.firstNum - this.firstNum; break; } case '*':{ this.result = this.firstNum * this.secondNum; break; } case '/':{ if(this.secondNum == 0) { throw new RuntimeException("被除数不能为0!"); } this.result = this.firstNum / this.secondNum; //四舍五入 this.result = new BigDecimal(this.result).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue(); break; } default: throw new RuntimeException("对不起,传入的运算符非法!"); } } } - calculator.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> <html> <head> <title>计算器</title> </head> <body style="text-align:center"> <jsp:useBean id="CalculatorBean" class="cn.itcast.CalculatorBean" scope="page"></jsp:useBean> <jsp:setProperty name="CalculatorBean" property="*" /> <% CalculatorBean.calculate(); %> <br /><hr /> 计算的结果是: <jsp:getProperty name="CalculatorBean" property="firstNum" /> <jsp:getProperty name="CalculatorBean" property="operator" /> <jsp:getProperty name="CalculatorBean" property="secondNum" /> = <jsp:getProperty name="CalculatorBean" property="result" /> <br /><hr /><br /> <form action="/day09_JavaBean/calculator.jsp" action="post"> <table border="1" width="50%"> <tr> <td colspan="2">简单的计算器</td> </tr> <tr> <td>第一个参数:</td> <td> <input type="text" name="firstNum" /> </td> </tr> <tr> <td>运算符</td> <td> <select name="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> </td> </tr> <tr> <td>第二个参数:</td> <td> <input type="text" name="secondNum" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="计算" /> </td> </tr> </table> </form> </body> </html> - 显示效果:
2、Servlet+JSP+JavaBean(MVC)
- 三层结构:
Cn.itcast.domain
Cn.itcast.dao
Cn.itcast.dao.impl
Cn.itcast.service
Cn.itcast.service.impl
Cn.itcast.web.controller
Cn.itcast.web.filter
Cn.itcast.web.listener
Cn.itcast.utils
Web-inf/jsp目录保存jsp文件
3、EL表达式
- el表达式:3.jsp:
<%@ page pageEncoding="UTF-8"%> <%@page import="cn.itcast.Address"%> <%@ page import="cn.itcast.Person" %> <%@ page import="java.util.*" %> <html> <head> <title>Insert title here</title> </head> <body> <% request.setAttribute("name", "aaa"); %> <!-- pageContext.findAttribute("name") page request session application--> ${name } <!-- 如果域对象中不存在标识符所对应的对象,则返回结果为””(注意,不是null) --> <!-- 在jsp页面中,使用el表达式可以获取bean的属性 --> <% Person p = new Person(); p.setAge(13); request.setAttribute("person", p); %> ${person.age } <!-- 在jsp页面中,使用el表达式可以获取bean中的。。。。。。。。。的属性 --> <% Person person = new Person(); Address address = new Address(); person.setAddress(address); request.setAttribute("person", person); %> ${person.address.name } <!-- 在jsp页面中,使用el表达式获取list集合中指定位置的数据 --> <% Person p1 = new Person(); Person p2 = new Person(); p1.setName("aa"); p2.setName("bb"); List l = new ArrayList(); l.add(p1); l.add(p2); request.setAttribute("list", l); %> ${list[1].name } <!-- 在jsp页面中,使用el表达式获取map集合的数据 --> <% Map map = new HashMap(); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("1", "11111"); request.setAttribute("map", map); %> ${map.a } ${map["1"] } <!-- 关键字为数字时的取法 --> <!-- 利用el表达式获取web应用的名称 href="/day09_JavaBean/..."--> <a href="${pageContext.request.contextPath }/calculator.jsp">跳到其他链接</a> <!-- 注意:如果访问bean不存在的属性,会抛 Property 'username' not found on type cn.itcast.Person --> <%-- ${person.nameeee }--%> </body> </html> - Addres.java:
package cn.itcast; public class Address { private String name = "aaaa"; public String getName() { return name; } public void setName(String name) { this.name = name; } }
4、JSTL标签+EL表达式
- 在页面中使用JSTL标签需完成以下2个步骤:
1、导入jstl.jar和standerd.jar这两个JSTL的jar文件。
2、在JSP页面中使用<%@ tablib uri="" prifix="" %>元素导入标签库。
JSTL标签库中常用标签:
<c:foreach var="" items="">
<c:if test=""> - jstl+el表达式示例代码。4.jsp:
<%@ page pageEncoding="UTF-8"%> <!-- url:standard.jar——>META-INF——>c.tld——><uri>http://java.sun.com/jsp/jstl/core</uri>; 快捷键:Alt+/ prefix前缀:可以瞎写。但是最好写找到url文件 的文件名的前缀:c --> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page import="cn.itcast.Person" %> <%@ page import="java.util.*" %> <html> <head> <title>jstl+el表达式示例代码</title> </head> <body> <!-- 使用jstl+el表达式可以迭代list集合 --> <% Person p1 = new Person(); Person p2 = new Person(); p1.setName("aa"); p2.setName("bb"); List l = new ArrayList(); l.add(p1); l.add(p2); request.setAttribute("list", l); %> <c:forEach var="person" items="${list}"> ${person.name}<br /> </c:forEach> <!-- 使用jstl+el表达式可以迭代map集合 --> <% Map map = new LinkedHashMap(); map.put("a", "aaaaa"); map.put("b", "bbbbb"); map.put("1", "11111"); request.setAttribute("map", map); %> <%-- Set<Map.Entry> set = map.entrySet()--%> <c:forEach var="mapentry" items="${map}"> ${mapentry.key} = ${mapentry.value}<br /> </c:forEach> <c:if test="${user!=null}"> 欢迎您:${user.username} </c:if> <c:if test="${user==null}"> 用户名:<input type="text" name="username" /><br /> 密码:<input type="password" name="password" /><br /> <input type="submit" value="登录" /> </c:if> </body> </html>
本文介绍了JSP开发模式,包括JSP+JavaBean实现的简单计算器案例,以及更复杂的Servlet+JSP+JavaBean(MVC)模式。此外,还探讨了EL表达式的使用方法,并结合JSTL标签进行了实例演示。
2013

被折叠的 条评论
为什么被折叠?



