这个章节主要讲解了struts标签的使用,结合小程序进行展示:
查找某范围内的质数:
其中想达到的目标是通过struts实现后台和显示界面同时变化的效果。
<!-- 表单数据输入项使用struts标签书写,才能绑定后台模型数据,从而实现模型动,界面也动,界面改了,实际就是就该模型。 -->
<!-- struts2通过数据标签,实现了界面跟后台模型绑定 -->
//value stack存储,struts主要把数据存储在属性中,很少存在请求范围里
//每个action方法都将返回一个字符串数据,该字符串就是一个result的名字,然后系统会自动在struts.xml中查找这个字符串所对应的资源
//struts2没有把路径写在代码中,实现了工作流的外贸部配置,比较灵活,修改路径,不需要修改源代码
//field error:是某个属性存在问题,
//action error:是整个action出了问题
<!-- Filters 过滤器 过滤器在系统启动的时候,会自动实例化,进入工作状态,根据过滤规则,对任何符合该规则的web访问进行过滤操作。
struts2就是通过一个过滤器执行任务的。 该过滤器工作的时候,主要查看该访问是否应该归struts2处理,如果是,则执行规则处理。否则忽略该访问
既定规则书写在struts.xml文件中,该文件如果没有特殊指定,只能放在src目录下。 -->
<!--
http://localhost:8080/strutsDemo/work/demo.do
1.struts过滤器拦截到url地址,获得该url的后缀,如果是*.do,则为struts处理范畴。去掉猴嘴,获得实体路径。
2.取得包路径(最后一个/之前的路径)/work
3.获得action的名字demo,然后找到这个action,创建该action的实例。执行execute方法一次。
-->
<!-- action是struts基本处理单位,用来代替servlet。其只能放在包中。
设置action,如果没有指定method的话,默认跑的是execute方法,如果有指定的话,则会执行制定的方法
-->
<!-- 如果没有特殊注明,restult默认都是forward提交 -->
代码粘贴:
find_prime.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'find_prime.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style> div{ margin-top:20px; } </style> <script type="text/javascript"> function validateForm(){ with(document.forms[0]){ //空值检测 if(beginScope.value==""){ alert("开始范围必须填写"); beginScope.focus(); return false; } if(endScope.value==""){ alert("结束范围必须填写"); endScope.focus(); return false; } //正整数检测 if(isNaN(beginScope.value)){ alert("开始范围必须填写数字"); beginScope.select(); return false; } if(isNaN(endScope.value)){ alert("结束范围必须填写数字"); endScope.select(); return false; } //小数检测 /* if(beginScope.value.split("\\.")>2){ alert("开始范围不能带有小数点"); beginScope.select(); return false; } if(endScope.value.split("\\.")>2){ alert("结束范围不能带有小数点"); endScope.select(); return false; }*/ } return true; }; </script> </head> <body> <h3>质数查询</h3> <s:form namespace="/math" action="findPrime" method="post" onsubmit="return validateForm();"> <div> <s:actionerror/> <span>开始范围:</span> <!-- 表单数据输入项使用struts标签书写,才能绑定后台模型数据,从而实现模型动,界面也动,界面改了,实际就是就该模型。 --> <!-- struts2通过数据标签,实现了界面跟后台模型绑定 --> <s:textfield name="beginScope"></s:textfield> </div> <s:fielderror> <s:param>beginScope</s:param> </s:fielderror> <div> <span>结束范围:</span> <s:textfield name="endScope"></s:textfield> </div> <s:fielderror> <s:param>endScope</s:param> </s:fielderror> <div> <span>显示风格:</span> <!-- # :在ognl表达式中,是Map构建符号,主要用来声明和构建一个key/value映射存储的hash表 --> <s:radio name="displayColor" list="#{'blue':'蓝色海洋','pink':'粉红玛丽','black':'无修饰'}"></s:radio><!-- ognl表达式, --> </div> <div> <span>额外功能</span> <s:checkboxlist name="otherFuns" list="#{'sum':'求质数和','cnt':'统计质数数量' }"></s:checkboxlist> </div> <div> <s:submit value="查找"></s:submit> </div> </s:form> </body> </html>
struts.xml 配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!-- /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <constant name="struts.devMode" value="false"></constant> <!-- 在网页中以开发模式打开,用于程序员直接在网页中查看运行情况 --> <constant name="struts.action.extension" value="do,action"></constant> <!-- 如果没有显示配置,默认为action, --> <constant name="struts.ui.theme" value="simple"></constant><!-- Struts 生成的UI界面代码风格,我们一般希望是自己能控制的。但是也可以一局各单位公司不同,有不同风格 --> <!-- http://localhost:8080/strutsDemo/work/demo.do 1.struts过滤器拦截到url地址,获得该url的后缀,如果是*.do,则为struts处理范畴。去掉猴嘴,获得实体路径。 2.取得包路径(最后一个/之前的路径)/work 3.获得action的名字demo,然后找到这个action,创建该action的实例。执行execute方法一次。 --> <package name="mathpkg" namespace="/math" extends="struts-default"> <!-- action是struts基本处理单位,用来代替servlet。其只能放在包中。 设置action,如果没有指定method的话,默认跑的是execute方法,如果有指定的话,则会执行制定的方法 --> <action name="inputPrimeScope" class="edu.fjnu.mathwork.action.MathAction" method="inputPrimeScope"> <!-- 如果没有特殊注明,restult默认都是forward提交 --> <result name="prime_input">/find_prime.jsp</result> </action> <action name="findPrime" class="edu.fjnu.mathwork.action.MathAction" method="findPrime"> <result name="prime_input">/find_prime.jsp</result> <result name="list_prime">/list_prime.jsp</result> </action> </package> </struts>
MathAction.java Action类;
/** * 工 程 名:MathWorkPrj-20180523 <br> * 文 件 名:MathAction.java <br> * 工具包名:edu.fjnu.mathwork.action <br> * 功能描述:TODO <br> * 创建时间:2018年5月23日 下午7:09:20 <br> * 版本信息:V1.0 * @创建人:周开伦 */ package edu.fjnu.mathwork.action; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import edu.fjnu.mathwork.service.MathService; import edu.fjnu.mathwork.service.MathServiceImpl; /** * 类名:MathAction <br> * 功能描述: <br> * 创建日期:2018年5月23日 下午7:09:20 <br> * 修改备注: * @作者信息:Zhou kailun <br> */ public class MathAction extends ActionSupport { //value stack存储,struts主要把数据存储在属性中,很少存在请求范围里 /**起始范围*/ private Integer beginScope; /**终止范围*/ private Integer endScope; /**显示风格*/ private String displayColor; /**额外功能*/ private String[] otherFuns; /** 最终查找到的结果*/ private List<Integer> primeList; /**<p>构造函数:</p><br><br> * <p>描述:</p><br> */ public MathAction() { // TODO Auto-generated constructor stub } public String execute()throws Exception{ System.out.println("MathAction invoked ok!"); return null; } public String inputPrimeScope()throws Exception{ this.beginScope=30; this.endScope=100; this.displayColor="black"; this.otherFuns=new String[]{"cnt"}; return "prime_input"; //每个action方法都将返回一个字符串数据,该字符串就是一个result的名字,然后系统会自动在struts.xml中查找这个字符串所对应的资源 //struts2没有把路径写在代码中,实现了工作流的外贸部配置,比较灵活,修改路径,不需要修改源代码 } public String findPrime()throws Exception{ //field error:是某个属性存在问题, //action error:是整个action出了问题 if(beginScope<30){ this.addFieldError("beginScope", "开始范围必须大于等于30"); return "prime_input"; } if(endScope>99999){ this.addFieldError("endScope", "结束范围范围必须小于99999"); return "prime_input"; } if(beginScope>endScope) { this.addActionError("开始范围不能大于结束范围"); return "prime_input"; } MathService mathService=new MathServiceImpl(); primeList=mathService.findScopedPrime(beginScope, endScope); return "list_prime"; } public Integer getBeginScope() { return beginScope; } public void setBeginScope(Integer beginScope) { this.beginScope = beginScope; } public Integer getEndScope() { return endScope; } public void setEndScope(Integer endScope) { this.endScope = endScope; } public String getDisplayColor() { return displayColor; } public void setDisplayColor(String displayColor) { this.displayColor = displayColor; } public String[] getOtherFuns() { return otherFuns; } public void setOtherFuns(String[] otherFuns) { this.otherFuns = otherFuns; } public List<Integer> getPrimeList() { return primeList; } public void setPrimeList(List<Integer> primeList) { this.primeList = primeList; } }
list_prime.jsp 显示质数信息类:需要完善功能目前部分展示
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'list_prime.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> span{ color:<s:property value="displayColor"/>; display:inline-block; width:50px; text-align:center; font-size:30px; } </style> </head> <body> <h3><s:property value="beginScope"/>--<s:property value="endScope"/>范围内的质数</h3> <s:iterator value="primeList"> <span><s:property/></span> </s:iterator> </body> </html>