`
isiqi
  • 浏览: 16354281 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

初学Struts2.0配置

阅读更多

web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 有时文件上传时common-io.jar,commons-fileupload-1.1.1.jar,有时会出错,加入org.apache.struts2.dispatcher.ActionContextCleanUp这个过滤器就行 -->

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

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>
<!-- 配置国际化信息 -->
<constant name="struts.custom.i18n.resources" value="messages"/>
<!-- 设置使用的解码集 -->
<constant name="struts.i18n.encoding" value="utf-8"/>


<package name="wang" extends="struts-default">
<action name="login" class="wang.actions.LoginAction">
<result name="input">/index.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/login.jsp</result>
</action>

<action name="upload" class="wang.actions.UploadFileAction">
<!-- 动态设置action的属性值 -->
<param name="savePath">d:/upload</param>
<result name="input">/upload.jsp</result>
<result name="success">/upload_suc.jsp</result>
</action>

</package>
</struts>

LoginAction文件:

package wang.actions;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private String userName;
private String userPwd;

//result
private String tip;
private String[] books;

public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public String getUserPwd() {
return userPwd;
}


public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}


public String getTip() {
return tip;
}


public void setTip(String tip) {
this.tip = tip;
}


public String[] getBooks() {
return books;
}


public void setBooks(String[] books) {
this.books = books;
}


public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
//以下相当于在session保存东西(session.setAttribute(key,value);
//页面中用 ${sessionScope.user}输出结果信息
ctx.getSession().put("user", this.getUserName());
ctx.getSession().put("pwd", this.getUserPwd());
/* //这种也可以
Map map = ctx.getSession();
map.put("user", this.getUserName());
map.put("pwd", this.getUserPwd());
*/
//以下相当于在request中保存东西(request.setAttribute(key,value);
//页面中用 ${requestScope.test}输出结果信息
ctx.put("test", "request put test");

//保存结果,页面中用<s:property value="tip"/>输出结果信息
//备注:页面中引入标签<%@taglib prefix="s" uri="/struts-tags"%>可以在struts2-core-2.0.11.1.jar\META-INF\struts-tags.tld中找到定义
this.setTip("Login OK!");
return this.SUCCESS;
}


public void validate() {
if(userName == null || userName.equals("")){ //fieldName 为表单域的名字
//this.addFieldError("userName", "User Name is NULL"); //也是OK
this.addFieldError("userName", this.getText("userName.required")); //国际化
//页面的国际化可用: '%{getText("messageKey")}'
}
if(userPwd == null || userPwd.equals("")){
this.addFieldError("userPwd", this.getText("userPwd.required"));
}

}


}

UploadFileAction文件:

package wang.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport {

private String title;

//封装上传文件域的属性
private File upload; //和表单域的name相同
//封装上传文件类型的属性
private String uploadContentType; //一定要是name+ContentType
//封装上传文件名的属性
private String uploadFileName; //一定要是name+FileName

//接受依赖注入的属性
private String savePath;


public String getSavePath() {
return savePath;
}
//接受依赖注入的方法
public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}



public String execute() throws Exception
{
System.out.println("开始上传单个文件-----------------------");
System.out.println(getSavePath());
System.out.println("==========" + getUploadFileName());
System.out.println("==========" + getUploadContentType());
System.out.println("==========" + getUpload());
//以服务器的文件保存地址和原文件名建立上传文件输出流
//自己的修改的:ServletActionContext.getServletContext().getRealPath("/UploadImages")
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
return SUCCESS;
}



}

index.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
This is my JSP page. <br>
<s:fielderror></s:fielderror>
<s:form action="login.action" method="post">
<s:textfield name="userName" label='%{getText("userName.label")}'></s:textfield>
<s:password name="userPwd" label="密码"></s:password>

<s:submit value="Login"></s:submit>
</s:form>

<form action="login.action" method="post">
UserName:<input type="text" name="userName">
UserPwd :<input type="password" name="userPwd">
<input type="submit" name="sbm" value="OK">
</form>

</body>
</html>

login.jsp文件:

输出结果2种方式如下:

<body>
<s:property value="tip"/>
${sessionScope.user}<br/>
${sessionScope.pwd}<br/>
${requestScope.test}<br/>

</body>

upload.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'upload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>
</html>

分享到:
评论

相关推荐

    struts2.0入门案例

    这个入门案例旨在帮助初学者快速理解Struts2.0的基本架构和配置。我们将创建一个简单的用户登录应用,包含一个登录表单和一个处理登录请求的Action。 三、环境配置 1. 安装JDK:确保你的开发环境已经安装了Java ...

    Struts2.0学习系列 以及 Struts2.0安装包

    这个"Struts2.0学习系列"旨在帮助初学者及进阶者深入理解并掌握Struts2.0的核心概念和技术。 在Struts2.0的学习过程中,首先需要了解的是其基本架构。Struts2.0框架整合了多种开源项目,如FreeMarker或JSP作为视图...

    Struts2.0 入门教程+帮助手册+权威指南+配置文档+标签库+增删改查代码等.rar

    这个压缩包文件提供了丰富的资源,包括入门教程、帮助手册、权威指南、配置文档以及标签库和实际操作的增删改查代码,为学习和掌握Struts2.0提供了全面的支持。 首先,`Struts2.0中文教程.chm`和`Struts2入门2.pdf`...

    struts2.0教程合集

    这个“struts2.0教程合集”包含了丰富的学习资源,帮助初学者和进阶者深入理解和掌握Struts2.0的核心概念、特性以及实践应用。 首先,"Struts+2+(中文PDF版).rar"很可能是Struts2.0的基础教程,以中文形式详细介绍...

    struts2.0学习教程PDF

    ### Struts2.0学习教程知识点详析 #### 一、Struts2.0概览与优势 **标题与描述解读:** "Struts2.0学习教程PDF"这一...无论是对于初学者还是有经验的开发者而言,掌握Struts2.0都是提升Web开发技能的重要途径之一。

    Struts2.0 入门学习资料

    "Struts2.0入门学习资料"可能包含教程文档、示例项目、视频课程等,这些资源可以帮助初学者快速了解Struts2.0的基本概念、配置方式以及实战技巧。"struts2.exe"可能是Struts2的开发环境或模拟运行环境,用于快速搭建...

    struts2.0基础和入门

    初学者应该通过阅读提供的文档,理解并编写Action、配置文件,尝试创建简单的Web应用,逐步掌握Struts2.0的核心技术和最佳实践。同时,了解其他相关技术如Spring、Hibernate等,将有助于构建更完整的Java Web开发...

    struts2.0工程源码(完整的struts2.0学习工程源码)

    struts2.0工程源码(完整的struts2.0学习工程源码) 这是一个完整的工程源码,包括所用到的jar包和发布配置文件。...导入到eclipse里几个运行,struts2.0入门学习工程,适合struts2.0广大爱好者和初学者学习和交流。

    struts2.0 教程(标签,XML配置,入门例子,帮助手册)

    本教程旨在帮助初学者和开发者掌握Struts2.0的核心概念、配置和实际应用。 首先,让我们深入理解Struts2.0的标签库。Struts2提供了丰富的自定义标签,如`&lt;s:property&gt;`用于显示对象属性,`&lt;s:form&gt;`用于创建表单,`...

    Struts2.0中文教

    本教程旨在帮助初学者逐步掌握Struts2.0的核心概念和技术,让你能够熟练运用这个框架构建功能丰富的Web应用程序。 首先,Struts2.0作为MVC框架,它的核心是Action类,它是业务逻辑处理的中心。每个Action对应一个...

    struts2.0中文教程

    通过这个中文教程,无论是初学者还是有经验的开发者,都可以系统地学习和提升在Struts2.0框架下的开发能力。教程中的实例代码和详细解释将帮助你快速上手,并在实际项目中游刃有余。请参考提供的`struts2.0中文教程....

    Struts2.0中文教程

    通过这个中文教程,无论是初学者还是有一定经验的开发者,都能深入理解Struts2.0框架,提升自己的Java Web开发能力。记得动手实践,结合实例来巩固理论知识,这样才能更好地掌握Struts2.0的精髓。

    Eclipse struts2.0可视化开发组件

    1. **可视化配置:** 用户可以通过图形界面配置Struts2.0的各种属性,而无需手动编写XML配置文件。 2. **代码生成:** 自动生成Struts2.0所需的Action类、配置文件等,大大减少了手工编码的工作量。 3. **项目模板:...

    Struts2.0中文教程.chm

    本教程全面覆盖了Struts2.0的核心概念和技术,为初学者和进阶开发者提供了深入的学习资源。 在Struts2.0中,其主要设计理念是通过使用拦截器(Interceptor)来实现业务逻辑和表现层的解耦。拦截器是一个实现了特定...

    struts2.0 经典书籍 源码 标签学习合集

    通过这些资源,无论是初学者还是有经验的开发者,都能系统地学习并掌握Struts2.0框架。书籍将提供理论知识,源码可以加深对框架机制的理解,而标签则是实际开发中的实用工具。总的来说,这份合集提供了全面的学习...

    struts2.0中文学习大全

    此外,文档还会讲解配置文件(struts.xml)的编写,这是配置Struts2.0行为的关键,包括Action映射、结果类型设定、拦截器链定义等。 其次,标签文档是前端展示的重要部分。Struts2.0提供了一套强大的标签库,简化了...

    最新struts2.0教程

    Struts2.0是Java Web开发中一个非常重要的框架,它是Apache软件基金会的Jakarta项目下的产品,主要用于构建MVC(Model-View-Controller)架构的Web应用。本教程涵盖了Struts2.0的核心概念、关键特性以及实际开发中的...

Global site tag (gtag.js) - Google Analytics