手动创建web项目练习struts2,目录结构:|-WEB-INF| |-classes| | |-struts.xml| | |-com| | | |-dao(Action.class)| | | |-action(LoginAction.class)| |-lib(struts2相关包)| |-web.xml|-login.html|-success.html|-error.htmlAction.java:
package com.dao;public interface Action {
public static final String SUCCESS = "success";
public static final String ERROR = "error"; public String execute() throws Exception;
}LoginAction.java:
package com.action;import com.dao.Action;public class LoginAction implements Action {
private String name;
private String pwd; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String execute() throws Exception {
String result = SUCCESS;
if (name.equals(pwd)) {
result = SUCCESS;
} else {
result = ERROR;
}
return result;
}
}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="pkg" namespace="/space" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">page/success.html</result>
<result name="error">page/error.html</result>
</action>
</package>
</struts>login.html:我的步骤是:
先手动编译Action.java,将得到的Action.class放入dao文件夹中,然后编译LoginAction.java,将得到的LoginAction.class放入action文件夹中,删除java文件,启动Tomcat,没有报错,输入
http://localhost:8080/struts2temp/page/login.html
后找不到login对应的Action: "There is no Action mapped for action name login"
package com.dao;public interface Action {
public static final String SUCCESS = "success";
public static final String ERROR = "error"; public String execute() throws Exception;
}LoginAction.java:
package com.action;import com.dao.Action;public class LoginAction implements Action {
private String name;
private String pwd; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String execute() throws Exception {
String result = SUCCESS;
if (name.equals(pwd)) {
result = SUCCESS;
} else {
result = ERROR;
}
return result;
}
}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="pkg" namespace="/space" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">page/success.html</result>
<result name="error">page/error.html</result>
</action>
</package>
</struts>login.html:我的步骤是:
先手动编译Action.java,将得到的Action.class放入dao文件夹中,然后编译LoginAction.java,将得到的LoginAction.class放入action文件夹中,删除java文件,启动Tomcat,没有报错,输入
http://localhost:8080/struts2temp/page/login.html
后找不到login对应的Action: "There is no Action mapped for action name login"
请把page去掉,不需要这样
http://localhost:8080/struts2temp/login.html
你把你的url贴出来看看。你package里面有个namespace,你访问的url里面有没有/space
如果没有肯定是错了
错了
应该是
http://localhost:8080/struts2temp/login.html
至于space应该不会有问题,用/的话连package都找不到的
我怀疑是纯粹的java手动导包问题,我的Java基础还是不牢啊。
访问的url为:http://localhost:8080/struts2temp/page/login.html
访问后的url为:http://localhost:8080/struts2temp/page/space/login.action
但是url应该为:http://localhost:8080/struts2temp/space/login.action才行,刚才我把页面都copy到struts2temp目录下,访问
http://localhost:8080/struts2temp/login.action
后url为:
http://localhost:8080/struts2temp/space/login.action
没问题了,但是我现在又有一个问题就是页面都放在page文件夹里要怎么处理才可以呢?
修改表单的action吗?
原来的action="space/login.action",
谁遇过这个问题了的,望指教!!!
<package name="pkg" namespace="/space" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">/page/success.html</result>
<result name="error">/page/error.html</result>
如果这样不行再在前面加/WEB-INF如
/WEB-INF/page/success.html