myeclipse 8.0 GA
1、新建空web project 名myweb
右击myweb-Myeclipse->Add Struts Capabiliti……->struts 2.1把名就改为了struts.xml
Finish
自动生成了struts.xml 添加struts-core……web.xml也自动添加了如下:
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
2、打开index.jsp添加代码如下:
<body>
<form action="login.action" method="post">
输入姓名:<input type="text" name="username"/>
<input type="submit" value="登录"/><br>
This is my JSP page. <br>
</form>
</body>3、在src下添加包action,在action包下添加类LoginAction.javapackage action;public class LoginAction {
private String username;
public String execute()throws Exception
{
if(getUsername().equals("zqq"))
{
return "success";
}
else
{
return "error";
}
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
}
}4、添加两个页面。一个error.jsp,一个welcome.jsp
修改 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
ok,部署tomcat
输入
http://localhost:8080/myweb/index.jsp打开后:对话框输入zqq结果:
type Status reportmessage No result defined for action action.LoginAction and result successdescription The requested resource (No result defined for action action.LoginAction and result success) is not available.
若打开index.jsp对话框输入不是zqq,则结果
type Status reportmessage No result defined for action action.LoginAction and result errordescription The requested resource (No result defined for action action.LoginAction and result error) is not available.
检查struts.xml有配置<result>,实在是找不到原因。。
1、新建空web project 名myweb
右击myweb-Myeclipse->Add Struts Capabiliti……->struts 2.1把名就改为了struts.xml
Finish
自动生成了struts.xml 添加struts-core……web.xml也自动添加了如下:
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
2、打开index.jsp添加代码如下:
<body>
<form action="login.action" method="post">
输入姓名:<input type="text" name="username"/>
<input type="submit" value="登录"/><br>
This is my JSP page. <br>
</form>
</body>3、在src下添加包action,在action包下添加类LoginAction.javapackage action;public class LoginAction {
private String username;
public String execute()throws Exception
{
if(getUsername().equals("zqq"))
{
return "success";
}
else
{
return "error";
}
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
}
}4、添加两个页面。一个error.jsp,一个welcome.jsp
修改 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
ok,部署tomcat
输入
http://localhost:8080/myweb/index.jsp打开后:对话框输入zqq结果:
type Status reportmessage No result defined for action action.LoginAction and result successdescription The requested resource (No result defined for action action.LoginAction and result success) is not available.
若打开index.jsp对话框输入不是zqq,则结果
type Status reportmessage No result defined for action action.LoginAction and result errordescription The requested resource (No result defined for action action.LoginAction and result error) is not available.
检查struts.xml有配置<result>,实在是找不到原因。。
<result>/WEB-INF/pages/error/errorJson.jsp</result>
<result name="error"></result>
</action>没配上面的红色区域吧
<package name="struts" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
<result>/welcome.jsp</result><result name="error">/error.jsp</result>
不写name=“success”,会默认是成功的页面。
把tomcat中项目重新加载几次,在action中return返回值前面,写几个打印语句System.out.println("aaa");调试一下,不是什么大的问题