首页 > 编程语言 > 详细

JSP_JavaBeanAndAction

时间:2015-03-23 11:41:13      阅读:226      评论:0      收藏:0      [点我收藏+]

实体:

public class Product {
	private String proName;
	private String proType;
	private float price;
	private int number;
	/**
	 * @return the proName
	 */
	public String getProName() {
		return proName;
	}
	/**
	 * @param proName the proName to set
	 */
	public void setProName(String proName) {
		this.proName = proName;
	}
	/**
	 * @return the proType
	 */
	public String getProType() {
		return proType;
	}
	/**
	 * @param proType the proType to set
	 */
	public void setProType(String proType) {
		this.proType = proType;
	}
	/**
	 * @return the price
	 */
	public float getPrice() {
		return price;
	}
	/**
	 * @param price the price to set
	 */
	public void setPrice(float price) {
		this.price = price;
	}
	/**
	 * @return the number
	 */
	public int getNumber() {
		return number;
	}
	/**
	 * @param number the number to set
	 */
	public void setNumber(int number) {
		this.number = number;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Product [proName=" + proName + ", proType=" + proType
				+ ", price=" + price + ", number=" + number + "]";
	}
	
	
}	

public class ProManager {
	public void printProductMsg(Product pro) {
		System.out.println(pro);
	}
}

public class Test {
		
	private String userName;
	
	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void print() {
		System.out.println("调用了方法");
	}
}

页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	// 设置编码格式
	request.setCharacterEncoding("UTF-8");
%>
<!-- 使用创建对象的动作 -->
<jsp:useBean id="pro" class="com.test.Product" scope="request" />
<!-- 通过setProperty动作设置值 -->
<%-- <jsp:setProperty property="proName" name="pro" value="电脑"/> --%>
<%-- <jsp:setProperty property="proName" name="pro" param="proName"/>
<jsp:setProperty property="proType" name="pro" param="proType"/>
<jsp:setProperty property="price" name="pro" param="price"/>
<jsp:setProperty property="number" name="pro" param="number"/> --%>

<!-- 通配符匹配属性【前提是表单元素的名称必须和属性名一致】-->
<jsp:setProperty property="*" name="pro"/>
<!-- 创建ProManager对象的实例 -->
<jsp:useBean id="proManager" class="com.test.ProManager" />
<%
    proManager.printProductMsg(pro);
	//request.getRequestDispatcher("show.jsp").forward(request, response);
%>

<!-- 跳转动作 [内部跳转动作]-->
<jsp:forward page="show.jsp" />

<!-- 保护动作 -->
<%-- <jsp:include page="" /> --%>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<fieldset>
		<legend>商品注册</legend>
		<form action="doRegister.jsp" method="post">
			<table>
				<tr>
					<th>商品名称:</th>
					<td><input type="text" name="proName1" /></td>
				</tr>
				<tr>
					<th>商品类型:</th>
					<td>
						<select name="proType">
							<option value="-1">==请选择==</option>
							<option value="生活品">生活用品</option>
							<option value="日活品">日用品</option>
							<option value="bu生活品">不日用品</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>价格:</th>
					<td><input type="text" name="price" /></td>
				</tr>
				<tr>
					<th>数量:</th>
					<td><input type="text" name="number" /></td>
				</tr>
				<tr>
					<td>
						<input type="submit" value="注册" />
					</td>
				</tr>
			</table>
		</form>
	</fieldset>
</body>
</html>



<%@page import="com.test.Product"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<!-- 
	如果在同一个范围中存储相同key值对象,直接使用该对象,否则重新创建新对象
 -->
<jsp:useBean id="pro" class="com.test.Product" scope="request" />
<body>
	<h1>商品信息如下:</h1>
	名称:<jsp:getProperty property="proName" name="pro"/><br/>
	类型:<jsp:getProperty property="proType" name="pro"/><br/>
	价格:<jsp:getProperty property="price" name="pro"/><br/>
	数量:<jsp:getProperty property="number" name="pro"/><br/>
	
	<%
		request.setAttribute("name", "张撒");
		request.setAttribute("name", "fdsfs");
	%>
	
	<%=request.getAttribute("name") %>
	
	<jsp:include page="Test.jsp" />
	
	<!-- 
	
		<%-- 
		
			<%@include file="" />
			<jsp:include page="" /> 
			1.使用指令会把包含页面内容放入当前页面在转译为java文件,而动作包含会把页面引用到当前页面
			2.指令只会生成一个java源文件,而动作会生成引用jsp页面转译源文件
		--%>
	 -->
</body>
</html>


<%@page import="com.test.Test"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- Test test = new Test(); 可以使用jsp动作代替 -->
<jsp:useBean id="test" class="com.test.Test" scope="page" /><!-- 创建对象动作 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		// 小脚本调用java类中的方法
		//Test test = new Test();
		test.print();
	%>
	
	<!-- 使用setproperty动作为对象的属性设置值 -->
	<jsp:setProperty property="userName" name="test" value="张三"/>
	<!-- 使用getProperty动作获取对象属性的值 -->
	<jsp:getProperty property="userName" name="test"/>
</body>
</html>



JSP_JavaBeanAndAction

原文:http://lhmjava.blog.51cto.com/9668287/1623281

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!