最近搭建了Struts2架構,發現了解了之後還是挺簡單的,也給新手做個向導吧
一、Struts2有什麼用?
答:(個人了解)就是對需要通路的網頁進行篩選。比如:你進入一個網站登入界面,輸入賬号密碼,點選登入,這個按鈕就會向背景傳回一個資訊,(舉例)如果傳回1代表成功,就跳轉到成功後的界面(如進入首頁),如果傳回0代表失敗,就跳轉到失敗後的界面(如重新輸入密碼界面)。仔細想想,這個功能該怎麼實作,這個時候就可以用到struts2。
二、首先看一下表結構(這個比較重要,尤其是對粗心的人來說)
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPnJmcW5GZpplMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL5ADN5ADN1ITMyETMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
三、經過的總結,搭建Struts2,可以細分為5步
1.建立動态web項目,建立的時候記得勾選下圖,導入Struts2的Java包
我這裡導入了11個jar包
你們直接把這些jar拉進lib檔案夾中
百度雲連結:Struts2 jar包 提取碼:pisk
2.建立hello.jsp,login.jsp, error.jsp
在WebContent中建立hello.jsp,登入成功界面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>問候程式</title>
</head>
<body>
你好
</body>
</html>
在WebContent中建立login.jsp, 登入界面 正确的賬号:kender 密碼:123
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登入頁面</title>
</head>
<body>
<form action="login" method="post">
使用者名:<input type="text" name="userName"><br/>
密 碼:<input type="password" name="password"/><br/>
<input type="submit" value="送出"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>
在WebContent中建立error.jsp,登入上失敗界面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>頁面錯誤</title>
</head>
<body>
輸入錯誤
</body>
</html>
3.配置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>Struts2_Test</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>Struts2Filter</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.配置HelloAction.java
在 src 下建立 com.struts2.hello 包,在包下面建立HelloAction.java
package com.struts2.hello;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction 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 execute(){
if(userName.equals("kender")||password.equals("123")) {
return SUCCESS;
}
else {
return ERROR;
}
}
}
5.配置struts.xml檔案
在 src 下建立 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="default" namespace="/" extends="struts-default">
<action name="login" class="com.struts2.hello.HelloAction">
<result name="success">/hello.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
在tomcat運作,
搭建過程中肯定會出現各種各樣的問題,都是過來人,大家都懂,我就奉上一句話:多查谷歌,沒谷歌的就查百度