webwork与spring集成

本文介绍了WebWork框架的基本配置方法及其实现登录功能的具体步骤。包括web.xml中ServletDispatcher和Spring ContextLoaderListener的配置、xwork.xml中的action定义、index.jsp页面的表单提交逻辑以及LoginAction类中的文件上传处理等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不想多说,直接代码

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>


xwork.xml配置

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">

<xwork>
<include file="webwork-default.xml"/>
<package name="default" extends="webwork-default">
<action name="login" class="loginBean">
<param name="path">/img</param>
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/index.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">
10240000
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</xwork>



index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@taglib prefix="ww" uri="/webwork"%>
<%
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>登陆窗口</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>
<fieldset>
<legend>登陆界面</legend>
<ww:form action="login" method="post" enctype="multipart/form-data">
<ww:fielderror></ww:fielderror>
<table style="height:60">
<tr>
<td><p style="font-size:14;color:#FF0000">用户名</p></td><td><input type ="text" name="user.username"></td>
<tr>
<tr>
<td>密码</td><td><input type ="password" name="user.password"></td>
</tr>
<tr><td>上传文件</td><td><input type="file" name="upload"></td></tr>
<tr><td><input type="submit" value="提交"></td></tr>
</table>
</ww:form>
</fieldset>

</body>
</html>



action配置

package com.test.xwork;

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

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionSupport;

public class LoginAction extends ActionSupport {
private User user;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String path;
private String localUploadFile;

public String getLocalUploadFile() {
return localUploadFile;
}

public void setLocalUploadFile(String localUploadFile) {
this.localUploadFile = localUploadFile;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}


public String execute() throws Exception {
if(user.getUsername().equals(user.getPassword())) {
fileUpload();
return SUCCESS;
}
return ERROR;
}

private void fileUpload() throws Exception {
FileInputStream read = new FileInputStream(upload);
setLocalUploadFile(ServletActionContext.getServletContext().getRealPath(getPath()) + "/" + uploadFileName);
FileOutputStream out = new FileOutputStream(getLocalUploadFile());

byte[] b = new byte[1024];
int i = 0;
while((i = read.read(b)) != -1) {
out.write(b, 0, i);
}
read.close();
out.close();
}
}



user class

package com.test.xwork;

public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}


applicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
<bean id="loginBean" class="com.test.xwork.LoginAction"></bean>
</beans>



最重要的一句话webwork.properties

webwork.objectFactory = spring

sucess.jsp页面

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib prefix="ww" uri="/webwork"%>

成功
<img name="testUpload" src="<ww:property value="localUploadFile"/>" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值