在eclipse中以struts2.5.22项目配置及使用

开发工具:
eclipse+tomcat(9.0)+struts2.5.22

首先从官网下载struts-2.5.22-all.zip压缩包,解压之。
在这里插入图片描述
然后新建一个空web项目
在这里插入图片描述

其次将struts-2.5.22-all的lib目录下的jar文件复制到web项目WEB-INF下的lib中
在这里插入图片描述

接着配置web.xml文件:

<?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" version="3.1">
  <display-name>struts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>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>
  
    <!-- Filters -->
  <!-- START SNIPPET: filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- END SNIPPET: filter -->

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

然后再src目录下建一个名为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>

 </struts>

然后我们就可以开始开发项目了!!

下面是演示实例:

1.再src下新建一个包,编写一个java文件:
在这里插入图片描述
代码如下:

package cn.javass.hello.struts2impl.action;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action{

	private String account;
	private String password;
	private String submitFlag;
	public String execute() throws Exception{
		System.out.println("用户输入的参数为==="+"account="+account+"password="+password+"submitFlag="+submitFlag);
		return "toWelcome";
	}
	public String getAccount()
	{
		return account;
	}
	public void setAccount(String account)
	{
		this.account=account;
	}
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password=password;
	}
	public String getSubmitFlag()
	{
		return submitFlag;
	}
	public void setSubmitFlag(String submitFlag)
	{
		this.submitFlag=submitFlag;
	}
}

2.然后编写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>
 
 <constant name="struts.devMode" value="true"/>
 <constant name="struts.locale" value="zh_CN"/>
 <constant name="struts.i18n.encoding" value="UTF-8"/>
 
 <package name="struts2" extends="struts-default">
   <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">
     <result name="toWelcome">/Text01/welcome.jsp</result>
   </action>
 </package>
 
 
 </struts>

3.再WebContent下建立一个Text01的文件夹,在其中编辑jsp文件:
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/struts2/helloworldAction.action" method="post">
  <input type="hidden" name="submitFlag" value="login"/>
  Account :<input type="text" name="account"/><br/>
  PassWord:<input type="password" name="password"/><br/>
  
  <input type="submit" value="submit">
</form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@taglib prefix="s" uri="/struts-tags" %>

欢迎账号为<s:property value="account"/>的朋友来访


</body>
</html>

运行结果:
在这里插入图片描述

成功!!

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小牧之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值