代码如下
login.jsp
<body>
 <s:i18n name="properties/messageResource">
       <s:text name="today is"/>
  <% java.util.Date now=new java.util.Date();
  pageContext.setAttribute("now",now); %>
  <s:date name="#attr.now" format="yyyy/MM/dd" nice="false"/>
  <hr>
  <s:form action="usrLoginAction.action" method="post">
   <h2><s:text name="loginTip"/></h2>
   <br/>
   <h4><s:textfield name="username" key="user"/></h4>
   <br/>
   <h4><s:password name="password" key="password"/></h4>
   <br/>
    <s:submit name="submit" key="submit"/>
    <br/>
    
   </s:form>
</s:i18n>
  </body>
UsrLoginAction.java
package com.ascent.action;
import java.util.ArrayList;
import com.ascent.anli.Usr;
import com.ascent.anli.UsrDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UsrLoginAction extends ActionSupport{
private String username;
private String password;
private String tip;
private ArrayList dataList;
@SuppressWarnings("rawtypes")
public ArrayList getDataList()
{
return dataList;
}
public void setDataList(ArrayList dataList)
{
this.dataList=dataList;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String getTip(){
return tip;
}
public void setTip(String tip){ 
this.tip=tip;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}
public void validate() {
//如果用户名为null,或者为空的话,那么提示错误信息
if(getUsername() == null || "".equals(getUsername().trim())){
this.addFieldError("username", this.getText("username.required"));
}

//如果密码为null,或者为空的话,那么提示错误信息
if(getPassword() == null || "".equals(getPassword().trim())){
this.addFieldError("password", this.getText("password.required"));
}
}
public String execute()throws Exception{
UsrDAO dao=new UsrDAO();
Usr u=dao.checkUsr(username, password);
if(u==null){
return "error";
}
else{
ActionContext.getContext().getSession().put("usr", u);
String superuser=u.getSuperuser();
if(superuser.equals("1")){
return "success1";
}else if(superuser.equals("2")){
return "success2";
}
else return "success3";
}
}
}
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>
<constant name="struts.custom.i18n.resource" value="properties/messageResource"/>
<package name="struts2" extends="struts-default">
<action name="findAllusrManagerAction" class="com.ascent.action.UsrManagerAction" >
<result name="showusr">/showusr.jsp</result>
</action>
<action name="usrLoginAction" class="com.ascent.action.UsrLoginAction">
 <result name="success1">/guest.jsp</result>
<result name="success2">/user.jsp</result>
<result name="success3">/admin.jsp</result>
<result name="error">/error.jsp</result> 
 </action>
</package></struts>要求是当登录名和密码为空时,会读取资源文件提示用户名和密码不能为空,但提交后却是404错误,不知哪里有问题,不为空是一切都正常,错误代码如下:
HTTP Status 404 - No result defined for action com.ascent.action.UsrLoginAction and result input--------------------------------------------------------------------------------type Status reportmessage No result defined for action com.ascent.action.UsrLoginAction and result inputdescription The requested resource (No result defined for action com.ascent.action.UsrLoginAction and result input) is not available.

解决方案 »

  1.   

    <action name="usrLoginAction" class="com.ascent.action.UsrLoginAction">
     <result name="success1">/guest.jsp</result>
    <result name="success2">/user.jsp</result>
    <result name="success3">/admin.jsp</result>
    <result name="error">/error.jsp</result> 
    加一个name="input"的result
      

  2.   

    struts校验失败后,默认result的name为input,所以不许配置result name=input
    如果不配置,就报这个错误的
      

  3.   

    没有return "showusr"吧.strust会在xml文件中依次遍历<result/>中的name属性值,并且在java文件中寻找与之对应的返回值,但是你的execute中没有一个返回值为"shower",(即使它没有用),于是会报错.可以吧这个action标签删掉或者添加一个返回值试试;