使用MyEclipse快速搭建一个struts2项目

从零开始搭建一个简单的struts2登录项目
*

工具:MyEclipse,简单登录

截图
在这里插入图片描述

1.首先需要导入jar包,

链接在这里:https://pan.baidu.com/s/1mFsIOfmfCnScYzAYtTZTyA
提取码: sx36

2.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" 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>Demo</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>*.action</url-pattern>
  </filter-mapping>
  
  <!-- 配置登陆配置 -->
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config>
</web-app>

3.struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
        <struts>
        	<package name="struts2"  extends="struts-default" namespace="/">
        		<action name="GoLogin" class="com.action.GoLogin" method="GoLogin">
        			<result name="success">/index.jsp</result>
        		</action>
        	</package>
        </struts>

4.action:GoLogin.java

package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class GoLogin extends ActionSupport{

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String GoLogin(){
		System.out.println("" + user);
		return SUCCESS;
	}
	
	
}

5.bean:User.java

package com.bean;

import java.io.Serializable;

public class User implements Serializable{
	//反序列化(防止实体类加载异常)
	private static final long serialVersionUID = 1L;
	
	private String name;
	private String pwd;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", pwd=" + pwd + "]";
	}
	
}

最后JSP,6.login.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>登录页面</title>

</head>

<body>
	<form action="GoLogin.action" method="post">
		<table>
			<tr>
				<th>用户名:</th>
				<th><input type="text" name="user.name"></th>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input type="text" name="user.pwd"></td>
			</tr>
			<tr>
				<td><input type="submit" value="登录"></td>
			</tr>
		</table>
	</form>
</body>
</html>

登录成功页面:

<body>
    登录成功!
</body>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值