1. 程式人生 > >Struts2一個簡單的登入跳轉

Struts2一個簡單的登入跳轉

所用工具:Myeclipse2014,Tomcat8.520,Struts2.3.16.1。(網上都有)。

1.開啟Meclipse2014

2,開啟Tomcat伺服器。F:\Struts\apache-tomcat-8.5.20-windows-x64\apache-tomcat-8.5.20\bin在bin目錄下面有一個startup.bat.(如果開啟只是一閃就自動退出的,百度,這裡不多講解。)

3.建立新專案,Web project.
1.在src右鍵創一個struts.xml檔案。在WebRoot->WEB-INF右鍵創一個web.xml檔案。2.在WEB-INF->lib放入tomcat.jar包,包的位置在F:\Struts\apache-tomcat-8.5.20-windows-x64\apache-tomcat-8.5.20\lib。(你下載Tomcat的lib目錄下)。全部複製過去就好!3

.
在WEB-INF右鍵創一個登入介面,一個登入成功介面Hello.jsp和lzt.jsp 4 .在src右鍵新建包,寫上包的名字,例如。com.struts.lzt在建立新類-Test1.。

如上程式碼!!
struts.xml程式碼:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd"
> <struts> <package name="default11" namespace="/" extends="struts-default"> <global-results> <result name="success"> /lzt.jsp </result> <result name="login"> /Hello.jsp </result> </global-results> <action name="hello" class = "com.struts.lzt.Test1"> </action
> </package> </struts>

(result的name決定程式跳轉到哪個jsp介面,action-name:是呼叫action時輸入路徑的組成部分,輸入網址的時候輸入它,會自動讀取此時result name ,然後跳轉到相應的介面。 action–class:action所在的路徑(包名+類名);)
Test1.java程式碼:

package com.struts.lzt;

import com.opensymphony.xwork2.ActionSupport;

public class Test1 extends ActionSupport{

    public String password;
    public String username;

    public void getUsername(String username){
         this.username=username;
    }

    public  String setUsername(){
        return username;
    }
    public void getPassword(String password){
         this.password=password;
    }

    public  String setPassword(){
        return password;
    }


    @Override
    public String execute(){
         if("11".equals(username) && "11".equals(password))
             return  "success";

         else 
             return "login";


    }
}

(給出相應輸入賬號密碼的 result-name)
web.xml程式碼:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

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

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

Hello.jsp程式碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--

    -->
  </head>

   <body>

   <s:form action="/hello" method="post">
    <s:label value="系統登陸"></s:label>
    <s:textfield name="username" label="賬號" />
    <s:password name="password" label="密碼" />
    <s:submit value="登入" />
   </s:form>

  </body>
</html>

(s:form action=”/hello” method=”post”這句話就是每次登入後會自動執行http://localhost:8080/專案名字/hello)
lzt.jsp程式碼:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'lzt.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
     success. <br>
  </body>
</html>

1.執行前,配置tomcat伺服器。在Myeclipse裡面配置.
點選Wimdow->Proferneces->Myeclipse->Serves->Tomcat 然後選擇版本,推薦最新版本 ,像我的話是Tomcat8.0,這裡寫圖片描述。Tomcat home directory 按Browse 選擇你安裝的Tomcar路徑。再選擇左上角的Enable ,按下apply。配置好了。
2.專案建立好後, 按下圖上所示圖示!這裡寫圖片描述

12312

選擇自己剛建立的專案,,點選Add,會出現下面的圖片,在Server上選擇自己的Tomcat版本。確定好,再按下右邊的圖片(配置圖示的右邊!),啟動Tomcat,成功的標誌是如下圖片。這裡寫圖片描述
(有點累,我有一篇文章就是寫這個的,複製累死我了)!

輸入的賬號密碼是在Test.java類裡面。
輸入正確的賬號密碼會跳轉到lzt.jsp介面。錯誤可以重新輸入(在原來介面)。

每天努力一點,加油!