eclipse开发struts2程序

本文介绍了一个使用Struts2框架在Eclipse环境下实现登录功能的例子。通过配置web.xml和struts.xml文件,并结合LoginAction类及登录界面,实现了基本的用户验证流程。

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


在eclipse下,用sturts2实现登录功能


代码结构如下:


[img]http://dl2.iteye.com/upload/attachment/0085/2013/bcdfc19c-fbb9-320f-99f5-da7f66d7f72c.jpg[/img]

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>struts</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>

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


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.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="*Action" class="com.action.struts.LoginAction" method="{1}">
<result name="success">/main.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>



LoginAction代码:

package com.action.struts;

import java.util.Map;

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

public class LoginAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

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

public String login(){
System.out.println("username :"+username);
System.out.println("password :"+password);
System.out.println("login method invoke");
if("yangjianzhou".equals(username)&&"123456".equals(password)){
Map session = ActionContext.getContext().getSession();
session.put("username", username);
return "success";
}
return "login";
}
}



login.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>login</title>
</head>
<body>
<div style="margin-top: 50px;" align="center">登录界面</div>
<form action="loginAction" method="post">
<table align="center" style="margin-top: 20px;">
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>


main.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>main</title>
</head>
<body>
<div style="margin-top: 50px;" align="center">
Welcome to home page : <%=(String)session.getAttribute("username") %></div>
</body>
</html>


运行界面:

[img]http://dl2.iteye.com/upload/attachment/0085/2015/ae15503a-ca71-399d-a55f-c8c6bc9531f6.jpg[/img]


[img]http://dl2.iteye.com/upload/attachment/0085/2018/b2973488-ffd4-3667-94ea-d6d9052503bb.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值