struts官网最新下载(struts2下载到指定路径)

本文目录
struts2下载到指定路径
/**
* @getDownloadFile
* 此方法对应的是struts.xml文件中的: 《param name="inputName"》downloadFile《/param》
*/
public InputStream getDownloadFile() throws FileNotFoundException {
//一系列指定下载名称或者其他操作
String path = 路径 + "/" + 包含后缀的文件名称;
// 此方法会下载到classPath下的文件夹
InputStream ins = ServletActionContext.getServletContext()
.getResourceAsStream(path);
//此方法为自己设置的路径
InputStream ins2 = new FileInputStream(new java.io.File(path));
//返回即可,返回哪种由你自己决定了
return ins2;
}
怎样在eclipse中添加struts框架
步骤如下:
一、下载struts包
百度直接搜“struts2”,进入Apache官网下载,此处下载的是struts-2.3.20-all.zip。
二、新建web项目
打开Eclipse新建一个Dynamic Web Project,项目名为:struts2。
三、向Web项目中添加struts2框架
1、解压struts-2.3.20-all.zip,在lib文件加下找到以下jar文件:
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.20.jar
xwork-core-2.3.20.jar
2、把以上文件,复制粘贴到第二步新建的Web项目下的WebContent/WEB-INF/lib路径下。
3、修改WebContent/WEB-INF路径下的web文件:
《?xml version="1.0"encoding="UTF-8"?》
***隐藏网址***
《display-name》struts 2《/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》
《!-- 以下是添加struts2框架 --》
《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》
四、建立struts2项目文件
1、在项目中的JavaResources/src路径下新建RegisterCheck.java跟RegisterAction.java文件(属于com.struts2包):
RegisterCheck.java代码:
package com.struts2;
publicclassRegisterCheck
{
publicboolean check(String psd,String repsd)
{
if (psd.equals(repsd))
{
returntrue;
}
else
{
returnfalse;
}
}
}
RegisterAction.java代码:
package com.struts2;
publicclassRegisterAction
{
private String usernaem;
private String psd;
private String repsd;
public String getUsernaem()
{
returnusernaem;
}
publicvoid setUsernaem(Stringusernaem)
{
this.usernaem = usernaem;
}
public String getPsd()
{
returnpsd;
}
publicvoid setPsd(String psd)
{
this.psd = psd;
}
public String getRepsd()
{
returnrepsd;
}
publicvoid setRepsd(String repsd)
{
this.repsd = repsd;
}
public String execute() throws Exception
{
RegisterCheckrc=newRegisterCheck();
if (rc.check(getPsd(),getRepsd()))
{
return"success";
}
else
{
return"failure";
}
}
2、在项目中的JavaResources/src路径下新建一个struts.xml文件,并在此文件中对项目的Action进行配置:
struts.xml文件内容:
《?xml version="1.0"encoding="UTF-8" ?》
《!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
***隐藏网址***
《struts》
《package name="struts2" extends="struts-default"》
《action name="register" class="com.struts2.RegisterAction"》
《result name="success"》/success.jsp《/result》
《result name="failure"》/failure.jsp《/result》
《/action》
《/package》
《/struts》
3、在项目下的WebContent目录下新建register.jsp、success.jsp和failure.jsp文件:
register.jsp代码:
《%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%》
***隐藏网址***
《html》
《head》
***隐藏网址***
《title》Insert title here《/title》
《/head》
《body》
《center》
《form action="register" method="post"》
用户名:《input type="text" name="username"》《br》
密码:《input type="password" name="psd"》《br》
确认密码:《input type="password" name="repsd"》《br》
《input type="submit" value="提交"》
《input type="reset"》
《/form》
《/center》
《/body》
《/html》
success.jsp代码:
《%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%》
***隐藏网址***
《html》
《head》
***隐藏网址***
《title》Insert title here《/title》
《/head》
《body》
《center》
《h1》注册成功《/h1》
《/center》
《/body》
《/html》
failure.jsp代码:
《%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%》
***隐藏网址***
《html》
《head》
***隐藏网址***
《title》Insert title here《/title》
《/head》
《body》
《center》
《h1》注册失败《/h1》
《/center》
《/body》
《/html》
五、运行Web项目
***隐藏网址***

更多文章:
avantage怎么设置为中文(Avantage安装丢失GIT)
2026年10月15日 03:00
activex控件未注册怎么办(登陆视频监控网页时提示activex控件未注册,怎么解决)
2026年10月14日 20:30
excel连接sql数据库教程(excel怎么连接sql数据库)
2026年10月14日 19:40






