关于Struts2配置的一些见解和总结

今天开始学习 Struts2 这个强大的 JAVA 开源框架,哎!不说了,配置了半天才跑起来。但总算有点收获。
首先,要跑起一个Struts2 的应用,一定要把Struts2的基础包导入。把以下5个包导进工程目录下的lib目录就可以了。用My Eclipse 的向导也可以导入。

1、commons-logging-1.0.4.jar
2、freemarker-2.3.8.jar
3、ognl-2.6.11.jar
4、struts2-core-2.0.11.jar
5、xwork-2.0.4.jar

然后就是在web.xml里面配置Struts2的主过滤器。在web.xml文件里加入如下代码:
<filter>
        <filter-name>Struts2</filter-name>
        <filter-class>
        org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

然后新建XML 命名为:“struts.xml” 为什么要这样做。因为这是Struts2框架的主要配置文件。所有请求都要在这里进行转发。
在 struts.xml 里键入以下代码

<?xml version="1.0" encoding="UTF-8"?>    /*这行说明这是一个XML 文件*/

/*这段说明引用 struts-2.0的标签库*/
<!DOCTYPE struts PUBLIC     
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

/*定义web请求和相应的请求对应的转发地址。*/
     <struts>
     <package name="strut2" extends="struts-default">
     <!-- 调用Action 默认的执行方法 execute() -->
     <action name="login" class="com.struts.test.loginAction" >
     <result name="success">/success.jsp</result>
     <result name="fail">/fail.jsp</result>
     </action>
    
      <!-- 调用Action 自定义的执行方法 regedit() -->
     <action name="regedit" class="com.struts.test.loginAction" method="regedit" >
     <result name="success">/success.jsp</result>
     <result name="fail">/fail.jsp</result>
     </action>
     </package>
     </struts>

把业务逻辑配置好之后,我们就开始制作视图资源了,记住,一般情况下,不要先做视图,再配置业务,遵循MVC 原则,模型驱动的核心思想。下面给出相对应的三个JSP 文件作为视图资源,当然,你可以用其他文件充当视图资源。

/*index.jsp*/


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!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  id="test_form" action="login.action">
<input type="text" name="uid"><br>
<input type="password" name="upass"><br>
<input type="submit" value="登陆" >
<input type="submit" value="注册">
<input type="reset" >
</form>
  </body>
</html>

注意: <form  id="test_form" action="login.action"> action属性是login.action .  "login" 是action的逻辑名,与struts.xml里面的 <action  name="login">要相同,然后该请求就按照返回值:

<result name="success">/success.jsp</result>
     <result name="fail">/fail.jsp</result>
返回相应的视图资源。返回success 就转跳到 success.jsp; 返回fail就转跳到 fail .jsp




/*success.jsp*/

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'success.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>success! <br>
  </body>
</html>



/*fail.jsp*/

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'fail.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>fail<br>
  </body>
</html>



   

posted on 2009-02-11 01:47 流浪汉 阅读(445) 评论(0)  编辑 收藏 引用 所属分类: javaweb

只有注册用户登录后才能发表评论。
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

公告

请你看完我的博客后,留下你对你看的日志或文章的一些意见和建议。

常用链接

留言簿

随笔分类

随笔档案

文章分类

新闻档案

相册

软件开发技术

搜索

最新评论

阅读排行榜

评论排行榜