struts2的搭建及第一个应用

来源:互联网 发布:nba录像 知乎 编辑:程序博客网 时间:2024/05/16 08:01
 

Struts2环境的搭建:

1、找到开发Struts2应用需要使用的jar文件

2、编写Struts2的配置文件

3、在web.xml中加入Struts2 MVC框架

 

 

Sstruts2案例

必要的准备

   struts2的开发环境。

      第一步:引入jar文件

   第二步:创建struts2的配置文件  如下

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

<struts>

</struts>

     第三步:添加启动 struts2所依赖的filter过滤器在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>

 

 

 第一步:在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">

<struts>

 

   <package name="test" extends="struts-default" namespace="/cs">

     <action name="hello" class="cn.csdn.hr.action.HelloAction" >

         <!-- result 返回的结果  -->

         <result name="login" type="dispatcher">../index.jsp</result>

     </action>

   </package>

  

</struts>

 

第二步:创建action

package cn.csdn.hr.action;

 

public class HelloAction {

   

   

    /*方法  第一必须 返回String字符串 默认的方法*/

    public String  execute(){

        System.out.println("--------------aciton执行----------------");

        return "login";

    }

 

}

 

第三步:分析:

http://localhost:8080/day11/cs/hello.action

../index.jsp

http://localhost:8080/day11/index.jsp

原创粉丝点击