莫名其妙的java struts2
写一个struts2入门的程序,从早上七点多,一直有错。action的代码如下:
}
import com.opensymphony.xwork2.ActionSupport;
public class FirstAction extends ActionSupport{
private int operand1;
private int operand2;
private int sum = 0;
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() {
System.out.println(sum);
sum = operand1 + operand2;
System.out.println(sum);
return sum;
}
public String execute() throws Exception {
if (getSum() >= 0) {
return "success";
} else {
return "failure";
}
}
}
struts的配置如下:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="sum" class="cn.edu.qust.struts.action.FirstAction">
<result name="success">/positive.jsp</result>
<result name="failure">/negative.jsp</result>
</action>
</package>
</struts>
web.xml代码如下:
<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.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
sum.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>输入操作数</title>
</head>
<body>
This is my JSP page. <br>
求代数和
<br/>
<s:form action="sum">
<s:textfield name="operand1"label="操作数1"></s:textfield>
<s:textfield name="operand2" label="操作数2"></s:textfield>
<s:submit value="代数和"/>
</s:form>
</body>
</html>
还有positive.jsp和negative.jsp。
在sum.jsp的界面中输入12和1进入positive.jsp的界面,然而奇妙的事情发生了输入-12和1也是进入positive.jsp生成的界面。这就让人郁闷了。
就在刚刚的15:10:47 。看到代码public class FirstAction extends ActionSupport上有自动提示的警告,根据自动提示加上了@SuppressWarnings("serial")。然后重新发布,竟然成功了!
觉得可能是自动添加的代码的关系,就把自动添加的代码去掉了,重新试了试。竟然也是成果的。这就让人郁闷了。不过既然代码现在没有错了。我也困了,去睡觉了,问题先写在这,以后想起来在研究之。
插下时间15:17:08
以上是 莫名其妙的java struts2 的全部内容, 来源链接: utcz.com/z/389642.html