<jsp:useBean id="user" class="UserData" scope="session"/>
<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username
SIZE=20 VALUE="<%= user.getUsername() %>"><BR>
What's your e-mail address? <INPUT TYPE=TEXT
NAME=email SIZE=20
VALUE="<%= user.getEmail() %>"><BR>
What's your age? <INPUT TYPE=TEXT NAME=age
SIZE=4 VALUE=<%= user.getAge() %>>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
比如来自Blazix服务器的标志库在你的系统环境中不能使用,那么不使用标志库来实现相似的功能呢?本教程就是为这个问题而提出的。这个问题是可以解决的,只是代码要长一点。主要地,你必须亲自编辑HTML标志,然后将它设置缺省值。从下面的例子的例子中我们可以学到怎样修改GetName.jsp以提供类似于blx:getProperty的特性,但是我们只能手工地编辑HTML标志:
如上面的代码所示,它只简单地在INPUT标志上增加一个“VALUE”区域,并且用一个表达是初始化这个区域。
为了处理在输入过程中的异常错误,我们可以在bean中使用“String”区域并且将它转换为目标数据类型就可实现。所以这样也可以处理异常错误。
作为课后练习,我们可以将前面教程中的例子改成不用Blazix标志库。
至此,本“JSP由浅入深”系列教程到此全部结束,希望对你有帮助。
下面的图是我用myeclipse建立的项目的目录。
目录结构有点多哈。。。没事其实很简单的!
下面是我的index.jsp中的全部代码:这里只是有一个表单,提交到TestServlet.java这个servlet中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| <%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
<h1>测试servlet</h1>
<hr>
<form action="servlet/TestServlet" method="post" dir="ltr">
<input name="userID" type="text"><br>
<input value="查询" type="submit"><br>
</form>
b.jsp
|
下面是TestServlet.java中的全部代码:这个是获取表单提交的内容,然后转发到另一个页面(c.jsp),其中这里包含一个业务逻辑(UserManager.java)
UserManager usermanager=new UserManager();
List userlist=usermanager.findUserByName(username);
以上这两行代码是业务逻辑的代码,获取数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| package com.majianjie.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//取得表单数据
String username=req.getParameter("username");
UserManager usermanager=new UserManager();
List userlist=usermanager.findUserByName(username);
req.setAttribute("userlist", userlist);//设置到req中,key-->value
//转向到一个界面c.jsp,取出list中的数据
req.getRequestDispatcher("/a/b/http://blog.csdn.net/majianjie/article/details/c/c.jsp").forward(req, res);//转发getRequestDispatcher
}
}
|
下面是UserManager.java 的代码行(即业务逻辑)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| package com.majianjie.servlet;
import java.util.ArrayList;
import java.util.List;
public class UserManager {
public List<string> findUserByName(String name){
List<string> userList=new ArrayList<string>();
userList.add("数据一");
userList.add("数据二");
userList.add("数据三");
return userList;
}
}
</string></string></string>
|
下面是c.jsp的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| <%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
c.jsp
<p>
<%
List userList = (List)request.getAttribute("userlist");
out.println(userList);
%>
</p><p>
a.jsp
</p>
|
下面是a.jsp的代码
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
a.jsp
下面是b.jsp的代码
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
b.jsp
a.jsp
c.jsp
d.jsp
e.jsp
d.jsp, e.jsp中的代码和a.jsp中的代码一样,唯一不同的就是: 将a改为d ,将a改为e......大部分代码是自动生成的,很省事的。
最后是web.xml中的代码:这里是一些配置信息。很重要的!!!嘿嘿嘿
- <span style="font-size:18px;"><form name="userForm" method="post" action="modifyuser.do?action=adduser" onSubmit="return userValidate();">
- <div class="MainDiv">
- <table width="99%" border="0" cellpadding="0" cellspacing="0" class="CContent">
- <tr>
- <th class="tablestyle_title" >人员信息录入</th>
- </tr>
- <tr>
- <td class="CPanel">
-
- <table width="90%" border="0" cellpadding="0" cellspacing="0" style="width:80%" align="center">
- <tr><td align="left">
- <input type="submit"value="保存" class="button"/>
-
- <input type="reset" value="返回" class="button"/>
- </td></tr>
-
- <TR>
- <TD width="100%">
- <fieldset style="height:100%;">
- <legend>人员信息</legend>
- <table width="100%" border="0" cellpadding="2" cellspacing="1" style="width:100%">
- <tr>
- <td nowrap align="right" width="9%">用户名:</td>
- <td width="36%">
- <input name="username" type="text" class="input" id="username"/>
- <span class="red">*</span></td>
- <td width="12%"><div align="right">登陆密码:</div></td>
- <td width="43%">
- <input name="password" type="text" class="input" id="password" />
- <span class="red">*</span></tr>
- <tr>
- <td nowrap align="right" width="9%">性别:</td>
- <td>
- <input name="sex" type="radio" value="1" checked> 男
- <input name="sex" type="radio" value="0">女</td>
- <td><div align="right">出生日期:</div></td>
- <td>
- <input name="birthday" type="text" class="input" id="birthday">
- <span class="red">*</span></td>
- </tr>
- <tr>
- <td nowrap align="right">是否管理员:</td>
- <td>
- <input name="isadminhelp" type="checkbox" onClick="javascript:adminChecked();" >
- <input type="hidden" name="isadmin" ></td>
- <td> </td>
- <td> </td>
- </tr>
- <tr>
- <td nowrap align="right">人员简介:</td>
- <td colspan="3">
- <textarea name="content" cols="100" rows="6" class="input" id="content"></textarea></td>
- </tr>
- </table>
- <br />
- </fieldset>
- </TD>
-
- </TR>
- </TABLE>
- </td>
- </tr>
- <TR>
- <TD colspan="2" align="center" height="50px">
- <input name="提交" type="submit" class="button" value="保存"/>
-
- <input name="重置" type="reset" class="button" value="重置"/></TD>
- </TR>
- </TABLE>
- </div>
- </form></span>
form的action="modifyuser.do?action=adduser"是一个虚假的url
它在struts.config.xml中通过 type="com.sanqing.action.UsersAction"设置它的表单交由谁处理
[html] view plain copy
- <action name="userForm" path="/modifyuser" scope="session" type="com.sanqing.action.UsersAction">
- <forward name="success" path="/user.do?action=listuser" />
- </action>
UserAction.java代码如下:
[java] view plain copy
- package com.sanqing.action;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.hibernate.HibernateException;
-
- import com.sanqing.dao.UsersDao;
- import com.sanqing.po.Users;
-
- public class UsersAction extends Action {
-
- private UsersDao dao=new UsersDao();
-
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) throws Exception {
- String action =request.getParameter("action");
- if(action==null||"".equals(action)){
- return mapping.findForward("error");
- }else if("listuser".equals(action)){
- return listUser(mapping,form,request,response);
- }else if("adduser".equals(action)){
- return addUsers(mapping,form,request,response);
- }else if("logon".equals(action)){
- return logon(mapping,form,request,response);
- }else if("updateuser".equals(action)){
- return updateUser(mapping,form,request,response);
- }else if("deleteuser".equals(action)){
- return deleteUser(mapping,form,request,response);
- }else if("selectuser".equals(action)){
- return selectUser(mapping,form,request,response);
- }
- return mapping.findForward("error");
- }
-
- private ActionForward selectUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
- Long id=new Long(request.getParameter("id"));//获得id参数
- Users u=dao.loadUsers(id);//加载该ID对应的人员信息
- request.setAttribute("user",u);//将人员信息设置到request范围
- return mapping.findForward("success");//跳转到success对应的逻辑视图
- }
-
- private ActionForward updateUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
- UsersForm usersform=(UsersForm)form;//获得UsersForm
- Users users=usersform.populate();//获得提交的人员信息
- dao.updateUsers(users);//更新人员信息
- return mapping.findForward("success");//跳转到success对应的逻辑视图
- }
-
- private ActionForward deleteUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
- Long id=new Long(request.getParameter("id"));//获得id参数
- Users users=new Users();//新建一个users对象
- users.setId(id);//设置该users对象的id
- dao.deleteUsers(users);//删除该users对象
- return mapping.findForward("success");
- }
- private ActionForward logon(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws HibernateException {
- UsersForm usersform=(UsersForm)form;
- Users users=new Users();
- users.setUsername(usersform.getUsername());
- users.setPassword(usersform.getPassword());
- boolean flag=dao.logonUsers(users);
- if(flag){
- request.getSession().setAttribute("users",users);
- return mapping.findForward("success");
- }else{
- request.setAttribute("error", "登录失败");
- return mapping.findForward("failed");
- }
- }
-
- private ActionForward addUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
- UsersForm usersform=(UsersForm)form; //获得UsersForm
- Users users=usersform.populate(); //获得提交的人员信息
- dao.addUsers(users); //完成人员信息保存
- return mapping.findForward("success"); //跳转到成功页面
- }
-
- private ActionForward listUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
- request.setAttribute("list",dao.listUser());//将人员信息列表保存到request范围
- return mapping.findForward("success"); //跳转到成功页面
- }
-
- }