struts2环境配置

流程:

  1. 导入jar包
  2. 配置web.xml
  3. 编写action类
  4. 配置struts.xml

1.使用eclipse创建项目

       File》New》Dynamic web project [记得选择生成web.xml文件,会在WebContent目录下生成]

2.在WebContent》WEB-INF>lib中导入包,并build path

      

  下载地址:https://download.csdn.net/download/douzhenwen/11225684

   注:一定要build path,鼠标移到包上右击鼠标会出现build path选项

3.在web.xml文件中配置struts过滤器(jsp页面在webcontent下新建jsp目录存放,故在web.xml中将index.jsp的路径修改)

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	 <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>jsp/index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
	<!-- 配置 Struts2 的 Filter -->
	<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>

 4.创建action类

package com.adu.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
    private String message;
    @Override
    public String execute() throws Exception {
        message = "Hello World,Struts2";
        return "success";
    }
    public String getMessage(){
        return message;
    }
}

5.配置struts.xml文件(在src目录下新建strut.xml文件,dtd约束不可少,即第二第三行的代码)

<?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="action" namespace="/" extends="struts-default">
        <action name="HWAction" class="com.adu.action.HelloWorld">
            <result name="success">jsp/first.jsp</result>
        </action>
    </package>
</struts>

说明:jsp目录下有两个jsp文件,第一个为index.jsp首页,第二个为first.jsp文件,index.jsp跳转到first.jsp

项目demo链接:https://download.csdn.net/download/douzhenwen/11225691

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值