下面是我index.jsp文件中的内容
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- <META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/HelloWorld.action">-->
</head><body>
<form action = "user" method = "post">
姓名:<input type = "text" name = "name"/>
年龄:<input type = "text" name = "age"/>
<input type = "submit" value = "submit">
</form>
</body>
</html>
struts.xml中的配置:
<?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="user" namespace="/" extends="struts-default">
<action name="user" class = "org.com.User">
<result>
/hello.jsp
</result>
</action>
</package>
</struts>User.java的程序:package org.com;
import com.opensymphony.xwork2.ActionSupport;
public class User extends ActionSupport
{ private int age;
private String name; public void setAge()
{ this.age = age;
}
public void setName(){
this.name = name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
} public String add(){
System.out.println("age = " + age);
System.out.println("name = " + name);
return "success";
} public String delete(){
return "success";
}
}hello.jsp的代码
<html>
<head>
<title>hello</title>
</head>
<body>
<h1>1111111111111111111111<h1>
</body>
</html>正常情况下,打开index.jsp文件,输入“姓名”和“年龄”后,Tomcat在后面会输出“neme=姓名”和“age=年龄”,才会跳转到hello.jsp页面中。但我做了好几次,Tomcat就不输出,直接跳转到了hello.jsp中。这是为什么呢?有哪位高手赐教,小弟是新手,望海涵!!!!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- <META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/HelloWorld.action">-->
</head><body>
<form action = "user" method = "post">
姓名:<input type = "text" name = "name"/>
年龄:<input type = "text" name = "age"/>
<input type = "submit" value = "submit">
</form>
</body>
</html>
struts.xml中的配置:
<?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="user" namespace="/" extends="struts-default">
<action name="user" class = "org.com.User">
<result>
/hello.jsp
</result>
</action>
</package>
</struts>User.java的程序:package org.com;
import com.opensymphony.xwork2.ActionSupport;
public class User extends ActionSupport
{ private int age;
private String name; public void setAge()
{ this.age = age;
}
public void setName(){
this.name = name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
} public String add(){
System.out.println("age = " + age);
System.out.println("name = " + name);
return "success";
} public String delete(){
return "success";
}
}hello.jsp的代码
<html>
<head>
<title>hello</title>
</head>
<body>
<h1>1111111111111111111111<h1>
</body>
</html>正常情况下,打开index.jsp文件,输入“姓名”和“年龄”后,Tomcat在后面会输出“neme=姓名”和“age=年龄”,才会跳转到hello.jsp页面中。但我做了好几次,Tomcat就不输出,直接跳转到了hello.jsp中。这是为什么呢?有哪位高手赐教,小弟是新手,望海涵!!!!!
好久不做web了 记不太清具体是啥了
我把index.jsp文件中的内容
<form action = "user" method = "post">
改成“<form action = "user!" method = "post">”
Tomcat也不会在后面打印出来提交的name 和 age啊!!!
<result name="success">/hello.jsp</result>
</action>
你得找个好点的例子照着写 struts2很多例子
<form action = "user" method = "post">
改成<form action = "User!add.action" method = "post">
<result name="success">
/hello.jsp
</result> 第二种方法是:
把这些代码
System.out.println("age = " + age);
System.out.println("name = " + name);
return "success";
Action的默认方法中,
也就是
public String execute()throws Exception{
System.out.println("age = " + age);
System.out.println("name = " + name);
return "success";}
我试了,如果是“/user"就会出现“The requested resource (/user!add) is not available.
”错误!!
我试了,
第一种方法得到:
There is no Action mapped for namespace [/] and action name [User] associated
的错误。第二种方法得到:
age = 0
name = null
public void setAge()
{ this.age = age;
}
public void setName(){
this.name = name;
}
你都把它设成空了 打印出来当然是那个结果
应该
public void setAge(int age)
{ this.age = age;
}
工具害死人了。
我也太马虎了!!!多谢大哥提点!!!
引用 13 楼 hfei99999 的回复:
你不觉得你得set方法很诡异么
public void setAge()
{ this.age = age;
}
public void setName(){
this.name = name;
}
你都把它设成空了 打印出来当然是那个结果
应该
public void setAge(int age)
{ this.age = age;
}
是,是这样的!!!高手果然是高手 !小弟佩服!!!!以前老是用eclipse自动生成set、get方法。现在用手写,居然写错了!!!
工具害死人了。
我也太马虎了!!!多谢大哥提点!!!