struts2入门---环境搭建

1.struts2环境搭建

下载: http://struts.apache.org/

我们这里直接选择这个版本的struts

安装:将struts2核心jar包导入web工程lib目录下

这里需要注意一个问题:

xwork-core-2.3.16.jar 2.5版本以前的版本都有这个,但在2.5版本就没有了.其实他们只是将它合并到struts2-core-2.5.12.jar里面了.

 接下来就是关键的了 struts2的配置

因为struts2是通过Filter来控制的.所以需要配置Filter.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2</display-name>
  
  <!-- 配置Struts2的Filter -->
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
web.xml

 现在就是使用struts了 配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">


<struts>
   <package name="default" extends="struts-default">
           <action name="default" class="com.struts.hello.helloWorld" method="execute">
               <result name="success">/hello.jsp</result>
           </action>
   </package>
</struts>
struts.xml

index.jsp

<a href="default.action">hello world</a>

现在只需要创建一个hello.jsp就可以进行测试了...

这样就用struts实现了控制页面的跳转逻辑...

---恢复内容结束---

1.struts2环境搭建

下载: http://struts.apache.org/

我们这里直接选择这个版本的struts

安装:将struts2核心jar包导入web工程lib目录下

这里需要注意一个问题:

xwork-core-2.3.16.jar 2.5版本以前的版本都有这个,但在2.5版本就没有了.其实他们只是将它合并到struts2-core-2.5.12.jar里面了.

 接下来就是关键的了 struts2的配置

因为struts2是通过Filter来控制的.所以需要配置Filter.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2</display-name>
  
  <!-- 配置Struts2的Filter -->
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
web.xml

 现在就是使用struts了 配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">


<struts>
   <package name="default" extends="struts-default">
           <action name="default" class="com.struts.hello.helloWorld" method="execute">
               <result name="success">/hello.jsp</result>
           </action>
   </package>
</struts>
struts.xml

index.jsp

<a href="default.action">hello world</a>

现在只需要创建一个hello.jsp就可以进行测试了...

这样就用struts实现了控制页面的跳转逻辑...

posted @ 2017-07-19 19:43  cx_davis  阅读(156)  评论(0编辑  收藏  举报