struts---No result defined for action org.crazyit.app.action.LoginAction and result error

来源:互联网 发布:linux查看内核版本 编辑:程序博客网 时间:2024/06/06 06:32

必须配上命名空间,第一种方法<s:form name=" register " action="/user/ register " >

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login" namespace="/user">
 <s:textfield name="username" key="user"/>
 <s:textfield name="password" key="pass"/>
 <s:submit key="login"/>
</s:form>
</body>
</html>

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>
 <!-- 指定全局国际化资源文件 -->
 <constant name="struts.custom.i18n.resources" value="mess"/>
 <!-- 指定国际化编码所使用的字符集 --> 
 <constant name="struts.i18n.encoding" value="UTF-8"/>
 <!-- 所有的Action定义都应该放在package下 -->
 <package name="lee" extends="struts-default" namespace="/user">
  <action name="login" class="org.crazyit.app.action.LoginAction">
   <!-- 定义三个逻辑视图和物理资源之间的映射 -->    
   <result name="error">/error.jsp</result>
   <result name="success">/welcome.jsp</result>
  </action>
 </package>
</struts>

package org.crazyit.app.action;

import com.opensymphony.xwork2.ActionContext;

//import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
 //定义封装请求参数的username和password属性
 private String username;
 private String password;
 
 public String getUsername()
 {
  return username;
 }
 public void setUsername(String username)
 {
  this.username = username;
 }
 
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 //定义处理用户请求的execute方法
 public String execute() throws Exception
 {
  //当username为crazyit.org,password为leegang时即登录成功
  if (getUsername().equals("crazyit.org")&& getPassword().equals("leegang") )
  {
   ActionContext.getContext().getSession().put("user" , getUsername());
   return "success";
  }
  else
  {
   return "error";
  }
 
 }
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title><s:text name="loginPage"/></title>
</head>
<body>
<s:form action="login" namespace="/user">
 <s:textfield name="username" key="user"/>
 <s:textfield name="password" key="pass"/>
 <s:submit key="login"/>
</s:form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
 pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title><s:text name="succPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
 <s:text name="succTip">
  <s:param>${sessionScope.user}</s:param>
 </s:text><br/>
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
 pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <title><s:text name="errorPage"/></title>
 <meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
 <s:text name="failTip"/>
</body>
</html>

 

 

原创粉丝点击