struts2入门程序

 

1、搭建struts2环境开发的步骤

搭建struts2环境时,我们一般需要做一下几个步骤的工作:

1、  创建javaweb工程

2、  找到开发struts应用所需要使用的jar

3、  创建jsp文件

4、  创建action文件

5、  编写struts2的配置文件

6、  在web.xml中加入struts2 MVC框架启动配置

 

开发struts2中需要使用的基本jar包:

 

关于struts2的提示:

我的myeclipes默认的struts2提示只到2.1,如果需要有2.3的提示,有两种情况:

1、  电脑可以连接外网:

在perference下面找xml,找到xml-catalog,选择User Specified Entries,add:

Location填入dtd声明中的http-那行也就是第二行,key填入第一行。外网会自动下载即可。

2、  电脑不可以连接外网:

将struts2.3的dtd解压防止在本地磁盘,不能有中文路径和空格,add中选择file system,其他不变。Location就是本地路径。

 

书写action:

 得到action我们有大概两种方式:

1、  类实现Action接口,至于什么jar就不多说了

2、  类继承自ActionSupport类

package cn.itcast.action;

import com.opensymphony.xwork2.ActionSupport;

/*
 
 public class HelloWorldAction implements Action {

    public String execute() throws Exception {
        System.out.println("this is HelloWorldAction execute method!");
        return "success";
    }

}
 
 */

public class HelloWorldAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    
    public String execute() throws Exception {
        System.out.println("this is HelloWorldAction execute method 2!");
        return "success";
    }
    
}

 

书写struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <!-- 书写配置文件找dtd,打开webApp,找到struts2.core下的2.3.dtd,直接复制这部分即可 -->

<struts>
    <!-- 
        package:包
        name:包名,唯一的,必选项
        namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是“/”
        extends:继承
            extends=“struts-default”:struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
            为什么要继承这个struts-default.xml文件
                因为struts框架底层提供的struts-default.xml声明了所有的拦截器和拦截器栈。
                我们知道struts2框架运行时执行struts-default.xml中的拦截器栈完成必要功能。
                如果不继承struts-default.xml文件,就没有办法使用struts2提供的所有拦截器。
        
     -->
    
    <!-- 动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 开发者模式 -->
    <constant name="struts.devMode" value="true" />

    <package name="one" namespace="/prima" extends="struts-default">
        <!--  
            action:
            name:对应页面请求链接的后面半部分
            class:对应要执行的类的完整路径
        -->
        
        <action name="userAction" class="cn.itcast.action.UserAction">
            
            <!-- 
            result:结果类型
                name:对应的是执行的类的方法的返回值
                 后半部分文本内容:转向的页面
            -->
            
            <result name="success">/success.jsp</result>
        </action>

    </package>
    
    <package name="two" namespace="/helloworld" extends="struts-default">
        <action name="helloWorldAction" class="cn.itcast.action.HelloWorldAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>

</struts>

 

配置struts2核心过滤器:

  

  <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>/*</url-pattern>
  </filter-mapping>

 

补充一些代码,可以自行拼接第一个struts程序:

package cn.itcast.action;

import com.opensymphony.xwork2.Action;

public class UserAction implements Action {

    public String execute() throws Exception {
        System.out.println("this is userAction execute method!");
        return "success";
    }

}
userAction
<%@ 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 'test.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>
    入门程序:<br/>
    <a href="${pageContext.request.contextPath}/prima/userAction.action">userAction</a><br/>
    <a href="${pageContext.request.contextPath }/helloworld/helloWorldAction.action">helloWorld</a>
  </body>
</html>
test.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 '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>
    this is success.jsp! <br>
    
    
  </body>
</html>
success.jsp

 

posted @ 2016-09-19 19:07  guodaxia  阅读(188)  评论(0编辑  收藏  举报