struts2基本配置

来源:互联网 发布:德比鞋 搭配 知乎 编辑:程序博客网 时间:2024/06/03 12:55

新建项目dynamic web project

项目名为struts

lib下导入.jar包(初学者全部导入(>_<))


web.xml:

配置了一个 Filter, 所有的请求都被过滤给了这个 Filter
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

<web-app>
  <display-name>struts</display-name>
   <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>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>        
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>


在src目录下新建一个业务控制Action类,

继承自com.opensymphony.xwork2.ActionSupport,

内容如下:

package com.action;
import com.opensymphony.xwork2.*;

public class HelloWorldAction extends ActionSupport{
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return SUCCESS;
    }
}


src目录下创建一个struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
  <package name="basicstruts" namespace="/" extends="struts-default">
  <action name="hello" class="com.action.HelloWorldAction">
    <result>hello.jsp</result>
  </action>    
</package>
</struts>


新建hello.jsp测试:


通过访问:

http://localhost:8080/struts/hello



整体思路:

1. 所有的访问都回被web.xml中配置的Struts 的 Filter所拦截
2. 拦截之后,就进入struts的工作流程
3. 访问的地址是/hello,根据struts按照 struts.xml中的配置,服务端跳转到hello.jsp
4. 显示hello.jsp的内容


原创粉丝点击