0.前言
学习了Struts框架的基本知识,接下来就是模拟实战,综合的利用Struts配置文件、标签和Action去开发实际功能。
1.题目
1.1 猜数字游戏
在一个页面选择猜测数字的范围,然后跳转到另一个页面输入猜测结果,并在此页面下方显示信息对错。
2.1 显示学生信息并删除
在一个页面用表格显示学生的详细信息,在每条信息后面都跟有删除链接。
3.1 挑选学生
display.jsp上可以挑选学生,被挑选的学生放到cart.jsp(有些类似于购物车),cart.jsp里面可以取消挑选
2.思路
1.两个JSP页面(create.jsp和guess.jsp),一个负责创建猜测范围,一个负责输入结果和显示结果。
2.
2.1选择范围页面
struts-config.xml:
<form-bean name="createForm" type="Action.CreateForm"></form-bean>
<action name="createForm" path="/create" type="Action.CreateAction"></action>
create.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>选择猜测数字的范围</title>
</head>
<body>
<html:form action="/create" method="get">
请您选择猜数字的范围:
<html:select property="rank">
<html:option value="10">0-10</html:option>
<html:option value="50">0-50</html:option>
<html:option value="101">0-100</html:option>
</html:select>
<html:submit value="创建随机数字" />
</html:form>
</body>
</html>
CreateForm.java:
package Action;
import org.apache.struts.action.ActionForm;
/**
* 作用:
*@author 欧世乐
*/
public class CreateForm extends ActionForm{
private String rank;
/**
* @return the rank
*/
public String getRank() {
return rank;
}
/**
* @param rank the rank to set
*/
public void setRank(String rank) {
this.rank = rank;
}
}
CreateAction.java:
package Action;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class CreateAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
CreateForm createForm =(CreateForm)form;
String str=createForm.getRank();
int rank=Integer.parseInt(str);
Random rnd=new Random();
int rndNumber=rnd.nextInt(rank);
HttpSession session=request.getSession();
session.setAttribute("rndNumber", rndNumber);
return new ActionForward("/guess.jsp");
}
}
2.2填写结果页面
struts-config.xml:
<action name="guessForm" path="/guess" type="Action.GuessAction"></action>
<form-bean name="guessForm" type="Action.GuessForm"></form-bean>
guess.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>输入猜测数字</title>
</head>
<body>
<html:form action="/guess" method="get">
请您输入一个数字来猜:
<html:text property="number"></html:text>
<html:submit value="提交猜测" />
</html:form>
<hr>
<logic:present name="message">
<bean:write name="message" />
</logic:present>
</body>
</html>
GuessForm.java:
package Action;
import org.apache.struts.action.ActionForm;
/**
* 作用:
*@author 欧世乐
*/
public class GuessForm extends ActionForm{
private String number;
/**
* @return the number
*/
public String getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(String number) {
this.number = number;
}
}
GuessAction.java:
package Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.sun.org.apache.bcel.internal.generic.RETURN;
import jdk.nashorn.internal.ir.RuntimeNode.Request;
/**
* 作用:
*@author 欧世乐
*/
public class GuessAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
GuessForm guessForm=(GuessForm)form;
String str=guessForm.getNumber();
int number=Integer.parseInt(str);
HttpSession session=request.getSession();
int rndNumber=(Integer)session.getAttribute("rndNumber");
String message="";
if(number>rndNumber) {
message="高了";
}
else if(number<rndNumber) {
message="低了";
}
else {
message="对了!";
}
request.setAttribute("message",message);
return new ActionForward("/guess.jsp");
}
}
2.3效果演示
3.题目二:思路
利用struts-config.xml配置action——query2,在地址栏输入query2.do,系统会自动调用query2配置的Action,在Action中调用学生的数据库信息,将结果存入request.setAttribute,这个时候相当于把ArayList存入request.setAttribute,然后调用结果页面(display.jsp)
在结果页面中,调用logic逻辑标签,自动获取request.getAttribute,再利用bean:write标签的property属性获取ArrayList的属性值,显示在页面上。
至于删除页面,使用struts的html:link标签,在struts-config.xml中定义action——delete。link标签中的action填写“/delete.do”,系统会自动去找到delete对应的Action,从数据库里面删除掉这为学生的信息,然后系统在自动跳转到/query2.do,相当于删除完毕后再一次执行查找操作。
所以页面实际上在按下删除的时候,就已经刷新一次了。
3.1 query.do的配置
struts-config.xml:
<action path="/query2" type="Action.Query2Action"></action>
这里的action无需name,因为无需ActionForm作为打包文件。
3.2 Query2Action.java:
package Action;
import java.util.ArrayList;
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 Dao.StudentDao;
/**
* 作用:
*@author 欧世乐
*/
public class Query2Action extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
StudentDao studentDao=new StudentDao();
ArrayList stus=studentDao.getAllInfo();
request.setAttribute("stus",stus);
return new ActionForward("/display.jsp");
}
}
3.3 display.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息</title>
</head>
<body>
显示结果<br>
<table bgcolor="ff80ff">
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>是否删除</td>
</tr>
<logic:iterate id="stus" name="stus">
<tr>
<td><bean:write name="stus" property="account"/></td>
<td><bean:write name="stus" property="name"/></td>
<td><bean:write name="stus" property="sex"/></td>
<td><bean:write name="stus" property="age"/></td>
<td><html:link action="/delete.do" paramId="STUS" paramName="stus" paramProperty="account">删除</html:link></td>
</tr>
</logic:iterate>
</table>
</body>
</html>
3.4 效果演示
4.题目三:思路
在浏览器输入“query3 .do”,系统会从数据库里面拿到所有学生的信息并且跳转到结果页面(display.jsp),选择挑选,就会触发"/select.do",系统会自动将该学生的信息放入session之中。
这里涉及到一个问题,如何从session之中获取学生的属性值【所有】属性值作为参数传递过去呢。答案是利用logic:iterate标签的indexId属性,将html:link的paramName设置为和indexId属性一样的值,当链接被点击,一次遍历里的所有信息就都被传递过去,接下来就是SelectAction的工作了。SelectAction利用requets.getAttribute获取ID值,转化为int类型,然后获取stus这个储存了全部学生信息的ArrayList,然后利用Object去找里面找到对应的id,最后把这个对应的id的内容全部存储到cart这个新建立的ArrayList里面。
在cart.jsp里面,获取名为cart的session即可。
当点击“取消挑选”后,会触发“delete2. do”,同样根据logic:iterate标签的indexId,html:link标签会将id值作为paramId传递到DeleteAction之中,DeleteAction获取id,转变为int类型,再次获取cart这个session,根据id,调用session.remove(id),就可以完成取消挑选这个操作了。
4.1struts-config.xml
<action path="/query3" type="Action.Query3Action"></action>
<action path="/select" type="Action.SelectAction"></action>
<action path="/delete2" type="Action.Delete2Action"></action>
4.1Query3Action
package Action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 Dao.StudentDao;
/**
* 作用:
*@author 欧世乐
*/
public class Query3Action extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
StudentDao studentDao=new StudentDao();
ArrayList stus=studentDao.getAllInfo();
HttpSession session=request.getSession();
session.setAttribute("stus", stus);
return new ActionForward("/display2.jsp");
}
}
4.2SelectAction
package Action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 作用:
*@author 欧世乐
*/
public class SelectAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String str=request.getParameter("ID");
int id=Integer.parseInt(str);
HttpSession session=request.getSession();
ArrayList stus=(ArrayList)session.getAttribute("stus");
Object selectObj=stus.get(id);
ArrayList cart=(ArrayList)session.getAttribute("cart");
if(cart==null) {
cart=new ArrayList();
session.setAttribute("cart", cart);
}
cart.add(selectObj);
return new ActionForward("/query3.do");
}
}
4.3 Delete2Action.java
package Action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class Delete2Action extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String str=request.getParameter("ID");
int id=Integer.parseInt(str);
HttpSession session=request.getSession();
ArrayList cart=(ArrayList)session.getAttribute("cart");
cart.remove(id);
return new ActionForward("/cart.jsp");
}
}
5.小结
多思考,多联系,会发现这些框架的优秀之处,加以学习。