From Gossip@caterpillar

JSP/Servlet: 視圖(JSP)



在視圖的展現部份,將看不到任何的scriptlet程式碼,這邊簡單的使用Expression Language來取出值呈現於頁面上,首先是welcome.jsp:
  • welcome.jsp
<%@page contentType="text/html;charset=Big5"%> 
<html>
<head><title>Welcome</title></head>
<body>
請先登入!<p>
<form name="userForm"
action="/model2Demo/login.action" method="post">
名稱 <input type="text" name="username"/><br>
密碼 <input type="password" name="password"/><p>
<input type="submit"/>
</form>
</body>
</html>


再來是驗證成功的頁面success.jsp:
  • success.jsp
<%@page contentType="text/html;charset=Big5"%> 
<html>
<head>
<title>Success</title>
</head>
<body>
<H1>Hello, ${ model['username'] } !</H1>
<H1>This is your secret data!!</H1>
<H1>${ model['secret'] } !!</H1>
</body>
</html>


最後是驗證失敗的頁面fail.jsp:
  • fail.jsp
<%@page contentType="text/html;charset=Big5"%> 
<html>
<head>
<title>Fail</title>
</head>
<body>
<H1>${ fail } !</H1>
<a href="welcome.action">重新登入</a>
</body>
</html>

這邊簡單的實作了一個Model 2架構的Web應用程式,當然了,實際上設計時要考量的還有許多,目前也有許多支持Model 2架構的框架(Framework),您並不一定要重新打造,先考慮一下這些框架是否合用,像是 Struts 等。