Struts框架的入门使用

1.struts框架的使用 

   导入jar包

   1.commons-fileupload-1.2.jar

   2. freemarker-2.3.15.jar

   3.ognl-2.7.3.jar 

   4.struts2-core-2.1.8.jar

   5.xwork-core-2.1.6.jar

  2.配置web.xml

     

  <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>

 

3.配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    
    
    <!-- 
    岁月可以褪去记忆,却褪不去一路留下的欢声笑语。2017,希望大家快乐继续,岁岁安怡
    
     -->
    
    <!-- 
        1、name指定包名,包与包之间的名称不能重复;
        2、namespace包命名空间,用于拼我们访问包的
            路径的,即URL。
            不要忘记/线
        3、extends指定包的继承关系的,这样我们可以
            方便的使用Struts2提供的默认配置。
     -->
    <package name="demo" 
        namespace="/hello" 
        extends="struts-default">
        <!-- 
            1、action是我们自己要写的业务组件,
                用于封装业务逻辑代码。
            2、name是指定action的名称的,
                该名称会用于我们访问action。
                同一包下可以有多个action,
                action名不能重复。
            3、class指定该action对应的业务组件,
                即我们自己定义的业务代码类。
            4、method指定要访问的方法名。
                这个method可以省略,若省略
                则默认调用execute方法。
            5、要访问当前的Action,其URL如下:
            http://localhost:8080/Struts2demo01/hello/hello.action
            http://ip:port/project_name/package_namespace/action_name.action
            注意:action名称的后缀.action可以省略的
         -->
        <action name="hello" 
            class="demo.HelloAction" 
            method="sayHello">
            <!-- 
                1、用result指定action处理完请求之后,
                    要去向的页面。
                2、name指定result的名称,
                    用于访问该result,
                    同一个action下可以有多个result,
                    他们之间不能重名。
             -->
            <result name="success">
                /WEB-INF/jsp/helloStruts.jsp
            </result>
            <result name="error">
                /WEB-INF/jsp/error.jsp
            </result>
        </action>
    </package>
    
</struts>

  4.写error.jsp 以及helloStruts.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
  This is a Error!
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
  Hello World!
</body>
</html>

  5.写HelloAction这个java类

package demo;

public class HelloAction {

    /**
     * 该方法在struts.xml配置过, 与配置文件相对应。
     */
    public String sayHello() {
        System.out.println("Hello,Struts!");
        /*
         * 返回的字符串与struts.xml中 定义的result对应,是用来找result的。
         */
        return "success";
    }
    
}

  运行

posted @ 2017-01-16 14:58  sujianbo  阅读(350)  评论(0编辑  收藏  举报