how to pass a Javabean to server In Model2 architecture.
What is model2 pattern ?
model2 pattern actually a pattern which use to construct web site , Before MVC pattern , we used Servlet + JSP + JavaBean to build web site .which we call this pattern
is model2.
First of first , Let start with a simple example of tutorial.
1. Defined the java bean like below .
package cn.javass.hello.servletimpl.vo; public class HelloWorldModel { private String account; private String password; private String submitFlag; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubmitFlag() { return submitFlag; } public void setSubmitFlag(String submitFlag) { this.submitFlag = submitFlag; } public String toString(){ return "account="+account+",password="+password +",submitFlag="+submitFlag; } public void businessExecute(){ System.out.println("正在进行业务处理=======>"); } }
2. implement the servlet .
package cn.javass.hello.servletimpl.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.javass.hello.servletimpl.vo.HelloWorldModel; public class HelloWorldServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ //1:收集参数,不用做了,通过JavaBean传入 //2:组织参数,也不用作了,已经组织好了,把数据封装成了JavaBean //这里只需要获取封装好的JavaBean即可 HelloWorldModel hwm = (HelloWorldModel)request.getAttribute("helloModel"); //3:调用模型的逻辑功能处理 hwm.businessExecute(); //这里简单的输出一下传入的参数 System.out.println("用户输入的参数为==="+hwm); //4:根据逻辑处理的结果来选择下一个页面,这里直接选择转向欢迎页面 //4.1:先把需要欢迎页面显示的数据准备好 request.setAttribute("hwm",hwm); //4.2:转向欢迎页面 request.getRequestDispatcher("/servletimpl/welcome.jsp").forward(request,response); } }
3. build jsp
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="gb2312"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Insert title here</title> </head> <body> <jsp:useBean id="helloModel" class="cn.javass.hello.servletimpl.vo.HelloWorldModel" scope="request"></jsp:useBean> <jsp:setProperty name="helloModel" property="*"/> <% if("login".equals(helloModel.getSubmitFlag())){ %> <jsp:forward page="/hello"></jsp:forward> <% } %> <form action="/helloworld/servletimpl/login.jsp" method="post"> <input type="hidden" name="submitFlag" value="login"/> 账号:<input type="text" name="account"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body> </html>
from this example . we can see servlet retrieve the JavaBean from the request .
we notice the code
HelloWorldModel hwm = (HelloWorldModel)request.getAttribute("helloModel");
is use to get JavaBean. We also can see the Javabean is specified in the Jsp.
<jsp:useBean id="helloModel" class="cn.javass.hello.servletimpl.vo.HelloWorldModel" scope="request"></jsp:useBean>
<jsp:setProperty name="helloModel" property="*"/>
let's get into the detail of these code .
the jsp:setProperty is set the value to the property of javabean instance.
property="*"
Stores all of the values the user enters in the viewable JSP page (called request parameters) in matching bean properties. The names of the properties in the bean must match the names of the request parameters, which are usually the elements of an HTML form. A bean property is usually defined by a variable declaration with matching getter and setter methods (for more information, see http://java.sun.com/products/javabeans/docs/).
What is the difference of getattribute and getparameter?
posted on 2012-12-24 22:16 malaikuangren 阅读(168) 评论(0) 编辑 收藏 举报