Struts2学习笔记——基本配置

本文详细介绍了如何使用Struts2框架进行Web开发,包括下载jar库、配置web.xml、编写action代码、创建相关jsp页面等步骤,以及实现登录功能。

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

首先下载Struts2的jar库。
地址:[url]http://struts.apache.org/download.cgi#struts232-SNAPSHOT[/url]
将需要的jar库拷贝到WEB-INF下的lib中。基本的9个包是:
commons-fileupload-xxx.jar
commons-io-xxx.jar
commons-lang-xxx.jar
commons-logging-xxx.jar
freemarker-xxx.jar
javasist-xxx.jar
ognl-xxx.jar
struts2-core-xxx.jar
xwork-core-xxx.jar

在web.xml中添加filter支持struts2
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

然后编写action代码:
package com.mm.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

private static final long serialVersionUID = -4543171546615125867L;

private String username;
private String password;

public String execute() {
if ("lander".equals(username) && "rover".equals(password)) {
return SUCCESS;
}
return LOGIN;
}

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;
}

}

接着在src下新建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>
<package name="struts2Test" extends="struts-default">
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<action name="login" class="com.mm.action.LoginAction">
<result name="success">/welcome.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>

这样就完成了一个action。
login.jsp:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="login">
<s:label value="登陆"></s:label>
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</body>
</html>

welcome.jsp:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
欢迎<s:property value="username"/>
</body>
</html>

一个简单但五脏齐全的struts2工程完成了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值