eclipse 編寫struct2的 demo

來源:互聯網
上載者:User

1、eclipse,mysql,apache tomcat的安裝和配置見前面幾篇文章

建一個Dynamic Web Project,取名叫OATest

2、struts2配置

下載struts2需要的的完整包,將其中的以下檔案複製到eclipse的對應web工程的WebContent/WEB-INF/lib的lib目錄下:

            1)xwork-core-            2)struts2-core-            3)ognl-            4)freemarker-            5)commons-io-
            6)commons-fileupload-
            7)javassist-

            8)commons-lang3-

3、struts2修改web.xml

     修改web.xml增加過濾器filter,最終設定檔如下所示

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.i18n.encoding" value="GBK"></constant><package name="default" extends="struts-default"><action name="loginAction" class="com.oa.action.LoginAction"><result name="success">welcome.jsp</result><result name="error">err.jsp</result></action></package></struts>

4、定義表單的JSP頁面

<%@ page language="java" contentType="text/html; charset=GBK"pageEncoding="utf-8"%><%@ taglib prefix="c" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><c:form action="loginAction"><c:textarea name="username" key="username"></c:textarea><c:textarea name="password" key="password"></c:textarea><input type="submit" value="提交"/></c:form></body></html>

5、定義攔截類com.oa.action.LoginAction

注意,包為com.oa.action

package com.oa.action;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private String username;private String password;public String execute() {if (username.equals("a") && password.equals("a"))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;}}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.