struts2 一些bug
来源:互联网 发布:arm linux 内核编译 编辑:程序博客网 时间:2024/06/05 19:08
2
package action;import com.opensymphony.xwork2.ActionSupport;public class FirstAction extends ActionSupport {private int operand1;private int operand2;public String execute() throws Exception {System.out.println(operand1);if (operand1+operand2 >= 0)// 如果代码数和是非负整数,跳到positive.jsp页面{return "positive";} else// 如果代码数和是负整数,跳到negative.jsp页面{return "negative";}}public int getOperand1() {return operand1;}public void setOperand1(int operand1) {System.out.println(operand1);this.operand1 = operand1;}public int getOperand2() {return operand2;}public void setOperand2(int operand2) {System.out.println(operand2);this.operand2 = operand2;}public int getSum() {return operand1 + operand2;// 计算两个整数的代码数和}}
这个是不可以的,因为在网络中传输的都是string
3
Integer.parseInt(operand1.toString())
也是不可以的,因为一次传输的是“1,10”无法转化为字符串。
包。要想正常使用Struts2,至少需要如下五个包(可能会因为Struts2的版本不同,包名略有差异,但包名的前半部是一样的)。
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
Struts2虽然在大版本号上是第二个版本,但基本上在配置和使用上已经完全颠覆了Struts1.x的方式(当然,Struts2仍然是基于MVC模式的,也是动作驱动的,可能这是唯一没变的东西)。Struts2实际上是在Webwork基础上构建起来的MVC框架。我们从Struts2的源代码中可以看到,有很多都是直接使用的xwork(Webwork的核心技术)的包。既然从技术上来说Struts2是全新的框架,那么就让我们来学习一下这个新的框架的使用方法。
如果大家使用过Struts1.x,应该对建立基于Struts1.x的Web程序的基本步骤非常清楚。让我们先来回顾一下建立基于Struts1.x的Web程序的基本步骤。
1. 安装Struts。由于Struts的入口点是ActionServlet,所以得在web.xml中配置一下这个Servlet。
2. 编写Action类(一般从org.apache.struts.action.Action类继承)。
3. 编写ActionForm类(一般从org.apache.struts.action.ActionForm类继承),这一步不是必须的,如果要接收客户端提交的数据,需要执行这一步。
4. 在struts-config.xml文件中配置Action和ActionForm。
5. 如果要采集用户录入的数据,一般需要编写若干JSP页面,并通过这些JSP页面中的form将数据提交给Action。
下面我们就按着编写struts1.x程序的这五步和struts2.x程序的编写过程一一对应,看看它们谁更“酷”。下面我们来编写一个基于Struts2的Web程序。这个程序的功能是让用户录入两个整数,并提交给一个Struts Action,并计算这两个数的代数和,如果代码和为非负数,则跳转到positive.jsp页面,否则跳转到negative.jsp页面。
【第1步】 安装Struts2
这一步对于Struts1.x和Struts2都是必须的,只是安装的方法不同。Struts1的入口点是一个Servlet,而Struts2的入口点是一个过滤器(Filter)。因此,Struts2要按过滤器的方式配置。下面是在web.xml中配置Struts2的代码:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
【第2步】 编写Action类
这一步和Struts1.x也必须进行。只是Struts1.x中的动作类必须从Action类中继承,而Struts2.x的动作类需要从com.opensymphony.xwork2.ActionSupport类继承。下面是计算两个整数代码和的Action类,代码如下:
package action;import com.opensymphony.xwork2.ActionSupport;public class FirstAction extends ActionSupport {private String operand1;private String operand2;public String execute() throws Exception {if (getSum() >= 0)// 如果代码数和是非负整数,跳到positive.jsp页面{return "positive";} else// 如果代码数和是负整数,跳到negative.jsp页面{return "negative";}}public String getOperand1() {return operand1;}public void setOperand1(String operand1) {this.operand1 = operand1;}public String getOperand2() {return operand2;}public void setOperand2(String operand2) {this.operand2 = operand2;}public int getSum() {return -10;}}
从上面的代码可以看出,动作类的一个特征就是要覆盖execute方法,只是Struts2的execute方法没有参数了,而Struts1.x的execute方法有四个参数。而且execute方法的返回值也不同的。Struts2只返回一个String,用于表述执行结果(就是一个标志)。上面代码的其他部分将在下面讲解。
【第3步】 编写ActionForm类
在本例中当然需要使用ActionForm了。在Struts1.x中,必须要单独建立一个ActionForm类(或是定义一个动作Form),而在Struts2中ActionForm和Action已经二合一了。从第二步的代码可以看出,后面的部分就是应该写在ActionForm类中的内容。所以在第2步,本例的ActionForm类已经编写完成(就是Action类的后半部分)。
【第4步】 配置Action类
这一步struts1.x和struts2.x都是必须的,只是在struts1.x中的配置文件一般叫struts-config.xml(当然也可以是其他的文件名),而且一般放到WEB-INF目录中。而在struts2.x中的配置文件一般为struts.xml,放到WEB-INF"classes目录中。下面是在struts.xml中配置动作类的代码:
这个东西防止在(src文件夹下面)<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><package name="struts2" namespace="" extends="struts-default"><action name="sum" class="action.FirstAction"> <result name="positive">/positive.jsp</result> <result name="negative">/negative.jsp</result></action></package></struts>
在<struts>标签中可以有多个<package>,第一个<package>可以指定一个Servlet访问路径(不包括动作名),如“/mystruts”。extends属性继承一个默认的配置文件“struts-default”,一般都继承于它,大家可以先不去管它。<action>标签中的name属性表示动作名,class表示动作类名。
<result>标签的name实际上就是execute方法返回的字符串,如果返回的是“positive”,就跳转到positive.jsp页面,如果是“negative”,就跳转到negative.jsp页面。在<struts>中可以有多个<package>,在<package>中可以有多个<action>。我们可以用如下的URL来访问这个动作:
http://localhost:8080/struts2/mystruts/sum.action
注:Struts1.x的动作一般都以.do结尾,而Struts2是以.action结尾。
【第5步】 编写用户录入接口(JSP页面)
1. 主界面(sum.jsp)
在Web根目录建立一个sum.jsp,代码如下:
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head> <title>输入操作数</title></head><body> 求代数和 <br/> <s:form action="sum.action" method="POST"><s:textfield name="operand1" label="操作数1" /> <s:textfield name="operand1" label="操作数2" /> <s:submit value="代数和"/> </s:form> </body> </html>
在sum.jsp中使用了Struts2带的tag。在Struts2中已经将Struts1.x的好几个标签库都统一了,在Struts2中只有一个标签库/struts-tags。这里面包含了所有的Struts2标签。但使用Struts2的标签大家要注意一下。在<s:form>中最好都使用Struts2标签,尽量不要用HTML或普通文本,大家可以将sum.jsp的代码改为如下的形式,看看会出现什么效果:
2. positive.jsp
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head>h1><s:property value=<title>显示代数和</title></head><body> 代数和为非负整数 <br/> <"sum"/></h1> </body> </html>
3. negative.jsp
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head>1><s:property value="<title>显示代数和</title></head><body> 代数和为负整数 <br/> <hsum"/></h1> </body> </html>
启动Tomcat后,在IE中输入如下的URL来测试这个例子:
http://localhost:8080/struts2/sum.jsp
- struts2 一些bug
- 一些bug
- 一些bug
- struts2文件下载bug
- struts2-2.1.6 bug
- 整理的一些Bug
- TTTAttributedLabel 中的一些bug
- Delphi的一些Bug
- duilib的一些bug
- tomcat linux 一些bug
- CSS的一些Bug
- ligerUI bug 一些解决办法
- 一些bug总结
- 一些bug及解决办法
- 关于一些常见bug
- 一些弱智BUG
- 常见的一些bug
- 一些评教bug
- GetProcAddress用法
- cygwin make:command not found
- linux 文本启动方式
- 嵌入式研发人员的核心竞争力浅谈
- Android中ExpandableListView的使用
- struts2 一些bug
- 动态规划解——有向图中的最长路径
- 相约世纪末
- HBase & Hadoop学习资料汇总
- V4L2接口命令说明及操作
- Quartz的用法实例一
- 深入浅出谈CUDA(一)
- Java数据库连接字符串
- myeclipse10.0安装配置SVN