从零开始搭建一个简单的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>