login.jsp页面代码:
<form action="Login" method="post">
用户名:<input type="text" name="name"></input><br>
密码:<input type="password" name="pwd"></input><br>
<input type="submit" value="提交"></input>
</form>LoginAction.java的代码:public class LoginAction extends ActionSupport{
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;
}
@Override
public String execute() throws Exception {
if(getName().equals("aaa") && getPwd().equals("aaa") ){
return "error";
}
else{
return SUCCESS;
}
}strutrs.xml的代码:<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="liu.action.LoginAction">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>点击提交就报错:
如下:HTTP Status 404 - /Myex/Login--------------------------------------------------------------------------------type Status reportmessage /Myex/Logindescription The requested resource (/Myex/Login) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.29
哪里出错了?
<form action="Login" method="post">
用户名:<input type="text" name="name"></input><br>
密码:<input type="password" name="pwd"></input><br>
<input type="submit" value="提交"></input>
</form>LoginAction.java的代码:public class LoginAction extends ActionSupport{
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;
}
@Override
public String execute() throws Exception {
if(getName().equals("aaa") && getPwd().equals("aaa") ){
return "error";
}
else{
return SUCCESS;
}
}strutrs.xml的代码:<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="liu.action.LoginAction">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>点击提交就报错:
如下:HTTP Status 404 - /Myex/Login--------------------------------------------------------------------------------type Status reportmessage /Myex/Logindescription The requested resource (/Myex/Login) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.29
哪里出错了?
<form action="Login" method="post">
action="Login" 改为 action="login" 试试..
而你表单提交的路径Login中L是大写的,当然404了,错误很明显
而且,你form表单中提交要加上.action,也就是login.action
没有连接上Login
<form action="login" method="post">
在jsp页面<form action="Login" method="post">
所以……
2.有个可以改进的地方:struts2自己有维护valueStack,所以getName().equals("aaa") && getPwd().equals("aaa") 可以直接写为: name.equals("aaa").....不需要调用get方法
We
在就是看web.xml中的配置对不对了
<display-name>Myex</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>如果你的struts版本在2.16一下。将org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 这个改成org.apache.struts2.dispatcher.FilterDispatcher你也可以两个都试试。
Java code <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<……
[/Quote]这个配置 再看!
将
<form action="Login" method="post">
1、改为<form action="login.action" method="post">
2、改为<s:form action="login" method="post"></s:form>
3、改strutrs.xml文件里的那个login为Login
<form action="login.action" method="post">否则会报404错误的!!