strut2 demo 示例 以及常见的错误处理

来源:互联网 发布:linux拷贝文件夹命令 编辑:程序博客网 时间:2024/06/01 08:47
首先说明一下示例的文档结构:
     strut2          //工程名称
       |-----src                         
         |-----cn.balance    //包名称 :随便换
|----LoginAction.java   //action类
 |----struts.xml    //strut2.0的配置文件:放在src 文件夹下,最终会被程序复制到WEB-INF的    //classes 文件夹下,
     ///////////////////////这个地方省略系统自动生成的项目
     |------WebRoot
|------META-INF
|------WEB-INF
|----lib
|----web.xml
|-----error.html
|-----index.jsp
|-----success.html

+下面是代码:

LoginAction.java:
/////////////////////////////////////////////////////////////////////////////////////
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

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;
}
public String execute () throws Exception
{
if(getUsername().equals("root")&&this.getPassword().equals("root")){
return "success";
}
else
{
return  "error";
}
}
}
//////////////////////////////////////////////////////////////////////////////

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 ="strut2" extends ="struts-default"  namespace="/strut" >
<action name ="login" class="cn.balance.LoginAction">
 <result name ="success" >/success.html</result>
<result name ="error">/error.html</result>
</action>
</package>
</struts>    


///////////////////////////////////////////////////////////////////////////
web.xml ---->本例为Myeclipse8.0GA创建,与手动创建的<filter-class>有所不同:
        此为另一种方式:  org.apache.struts2.dispatcher.FilterDispatcher
请视具体strut2的版本,情况而定 (两个都一样)
/////////////////////////////////////////////////////////////////////////
<?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>*.action</url-pattern>
  </filter-mapping></web-app>

////////////////////////////////////////////////////////////////////////

error.html
//////////////////////////////////////////////////////////////////////
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>error.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    Name or Password error!But  Your demo still sucess!!! Congratulations!
  </body>
</html>


//////////////////////////////////////////////////////////////////////
success.html
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>success.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    success!Congratulations!
  </body>
</html>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
index.jsp
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.print(basePath);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <form  action="strut/login.action" >  
     Name:<input type="text" name ="username"><br>
    Password: <input type="password" name="password"><br>
    <input type="submit"value ="Submit">    
     
    </form>
    
  </body>
</html>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
↑ 好了以上就是程序的全部代码 ↑ 下面说一下常见得错误:
错误:
There is no Action mapped for namespace / and action name login

首先出现这种错误是因为配置文件没找到,检查一下你的struts.xml是否在classes 文件夹下
路径:“安装Tomcat 的路径”\webapps\“你工程的名称”\WEB-INF\classes

其次,查看你的struts.xml这个文件名称是否与“struts.xml”一样,并且检查其中内容 是否正确,每一个属性都要检查
注意拼写错误!!!!!!

再次,如果没用<s:form>的话 形式应该是这样:<form  action="strut/login.action" >  
如果你用的是<s:form >形式 那应该是:<s:form name ="form1" action ="login" namespace="/strut">

↓↓↓要记住↓↓↓
form 标签  没有namespace属性
s:form 标签  有namespace属性
………………………………↑曾经因为这个蛋疼两天
运行起来 成功就出现 :
success!Congratulations!!
用户名或密码错误出现:(本例用户名密码都是root)
Name or Password error!But  Your demo still sucess!!! Congratulations!

原创粉丝点击