一、创建项目
首先我们用idea创建一个Javaweb项目,选择idea本身自带的struts2(因为版本号较高,网上的一些demo可能不能直接用,需要修改,也可以自己导入相应的低版本的struts2框架)
二、导入相应的jar包
这里我没有一个一个下载相应的jar包,直接采用了maven依赖下载,避免了版本不对应,不会maven导包的,可以看我之前的ssm配置详解,那里面有教程。
三、编写配置文件
web.xml中我们需要配置过滤器,注意如果你的struts版本在2.3之后你的filter-class为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml中我们暂时只要写入固定格式,注意如果你的struts版本要与配置的一至,否则会出现一系列的问题
<?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>
</struts>
四、测试一下服务器
一般来说服务器里面是没有相应的struts的jar包的,因此需要你手动的将struts以及他依赖的jar包导入到tomcat的lib中。导入之后我们运行服务器。发现没问题之后,我们就用struts框架写一个登录demo,体验一下struts2。
五、struts运用
在src目录下新建package,创建你的action,这里我们做一个登录程序,新建loginAction.java
loginAction.java
package com.huzai.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class loginAction extends ActionSupport {
private String username;
private String password;
public String execute(){
if(getUsername().equals("huzai")
&&getPassword().equals("123456"))
{
ActionContext.getContext().getSession()
.put("user", getUsername());
return SUCCESS;
}
return ERROR;
}
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;
}
}
写好Action后在sruts.xml中进行配置具体如下
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="struts_001" namespace="/" extends="struts-default">
<action name="login" class="com.huzai.action.loginAction">
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
完成后编写jsp展示界面,包括login.jsp、index.jsp、error.jsp
login.jsp
<%--
Created by IntelliJ IDEA.
User: mi
Date: 2018/9/18
Time: 17:30
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>login</title>
</head>
<body>
<div align="center" >
<form action="login.action" >
用户名 :<input type="text" name="username"/><br/>
密 码 :<input type="password" name="password"/><br/>
<input type="submit"value="登录"/>
</form>
</div>
</body>
</html>
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: grays
Date: 2018/10/9
Time: 22:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1 align="center">
欢迎你,${user}!
</h1>
</body>
</html>
error.jsp
<%--
Created by IntelliJ IDEA.
User: mi
Date: 2018/9/18
Time: 17:39
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>error</title>
</head>
<body>
<h2 align="center">用户名或密码错误请重新登录!</h2>
</body>
</html>
至此,一个登录的demo就完成了,运行一下服务器,看看效果
六、最终效果
login界面
登录成功后界面
登录失败界面