Struts2起步

来源:互联网 发布:淘宝amp服务商是什么 编辑:程序博客网 时间:2024/05/22 03:16

项目结构图

这里写图片描述

第一步 导入jar包

导入apps文件夹中blank项目所用jar包,没必要将struts的所有jar包全部倒入,以后需要其他的再导入相应的即可

第二步 配置过滤器

可以从blank项目的web.xml文件中拷贝

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>struts2_01</display-name>    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list>    <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></web-app>

第三步 创建action

action就是一个POJO类

package cap.action;public class HelloAction {    public String execute(){        System.out.println("Hello Struts2");        return "success";    }}

第四步 配置struts的配置文件

在src目录中创建struts.xml,可以从blank项目中classes文件夹下拷贝同名文件进行修改

<?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>    <!-- name值为当前包的名称,定义继承关系的时候会用到,可以自定义,默认是default -->    <!-- namespace的值与URL有关,如果namespace="/say",那么URL就应该为http://localhost:8080/struts2_01/say/hello.action -->    <!-- extends定义包的继承关系 -->    <package name="default" namespace="/" extends="struts-default">        <action name="hello" class="cap.action.HelloAction">    <!-- 在路径中输入hello,系统会到cap.action.HelloAction中默认执行execute方法,然后根据返回结果判断由哪个页面处理 -->            <result name="success">/hello.jsp</result>        </action>    </package></struts>
0 0
原创粉丝点击