酷知百科網

位置:首頁 > 遊戲數碼 > 電腦

如何搭建struts2項目

電腦1.63W

搭建struts2項目的一些簡單易於理解的操作

如何搭建struts2項目

操作方法

(01)從網上下載struts2需要的jar包,以下是需要用到的jar包

如何搭建struts2項目 第2張

(02)1.在myeclipse中新建web工程,隨便起名字,例如我的是叫logintest

如何搭建struts2項目 第3張

(03)2.匯入需要引用的jar包,在logintest項目上右鍵->build path->configure bulid path,點擊libraries,點擊add external jars...,選擇你的struts2需要用到的jar包的位置添加進來,點擊ok

如何搭建struts2項目 第4張
如何搭建struts2項目 第5張
如何搭建struts2項目 第6張

(04)3.在WebRoot下找到檔案進行配置,將下面的代碼粘貼過去<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns=""xmlns:xsi=""xsi:schemaLocation="://_2_"><!-- 定義struts2的核心filter即struts2的FilterDispatcher的filter --><filter><filter-name>struts2</filter-name><!-- 定義核心filter的名稱 --><filter-class><!-- 定義核心filter的實現類 -->erDispatcher</filter-class></filter><!-- 核心filter攔截所有請求 即FilterDispatcher用來初始化struts2並且處理所有的web請求--><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

(05)在src裏面新建一個xml檔案命名爲,然後將下面的代碼粘貼到此檔案中<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"""><struts><!-- 若package加上namespace屬性,則jsp頁面form 的action屬性必需加上項目名稱的路徑--><package name="struts2" extends="struts-default" namespace="/demo"><action name="login" class="nAction" ><result>/</result></action></package></struts>

(06)在src中新建一個包,命名爲on,在此包下新建一個類命名爲LoginAction,將下面的代碼站提過去package on;import onSupport;public class LoginAction extends ActionSupport{private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {name = username;}public String getPassword() {return password;}public void setPassword(String password) {word = password;}public String execute(){tln("走進action");return SUCCESS;}public String login(){return SUCCESS;}}

(07)在WebRoot下新建一個jsp,命名爲,將下面的代碼複製過去<%@ page language="java" import=".*" pageEncoding="UTF-8"%><%String path = ontextPath();String basePath = cheme()+"://"+erverName()+":"+erverPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My 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="">--></head><body><form action="/attendance_book/demo/on" method="post">賬號:<input type="text" name="username"/><br/>密碼:<input type="text" name="password"/><br/><input type="submit" value="確定"></form></body></html>

(08)在WebRoot下新建一個jsp,命名爲,將下面的代碼複製過去<%@ page language="java" import=".*" pageEncoding="UTF-8"%><%String path = ontextPath();String basePath = cheme()+"://"+erverName()+":"+erverPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My 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="">--></head><body>用戶名:${name}密    碼:${word}</body></html>

(09)將此項目添加到tomcat中,啓動tomcat,在地址欄輸入地址:,

如何搭建struts2項目 第7張

(10)在此文字方塊中輸入用戶名和密碼,會跳到下面的頁面

如何搭建struts2項目 第8張

特別提示

檔案是放在src目錄下的,當然也可以放在別的地方,但是得在中配置一下

它的原理是這樣的,首先在你點擊登入,它根據中的內容找到,在裏面找到方法爲login的

然後後面是對應的類,他就找到這個類,然後這個類裏面找到exectu方法,或者別的,可以自訂,然後返回的是success,它在回到中,找到result中success對應的jsp

標籤:struts2 搭建 項目