struts2的簡單實例

先看我的項目結構


1、準備jar包

commons-fileupload-1.3.jar、commons-io-2.2.jar、commons-lang3-3.1.jar、commons-logging-1.1.3.jar、freemarker-2.3.19.jar、javassist-3.11.0.GA.jar、ognl-3.0.6.jar、struts2-core-2.3.16.jar、xwork-core-2.3.16.jar

2、web.xml文件中加入

	<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>
3、準備index.jsp頁面

<%@page pageEncoding="utf-8"%>

<html>
	<head>
		<title>歡迎頁面</title>
	</head>
	<body>
		<form action="${pageContext.request.contextPath}/test.action" method="post" >
			<input type="text" name="userName" value="" autocomplete="off"><br>
			<input type="password" name="userPswd" value="" ><br>
			<input type="submit" value="提交">
		</form>
		
	</body>
</html>

4、給src下面加入struts.xml文件,配置如下:

	<package name="me" namespace="/" extends="struts-default">
        <action name="test" class="com.hhj.test.StrutsAction" method="test">
            <result name="success">WEB-INF/test/success.jsp</result>
            <result name="error">WEB-INF/test/error.jsp</result>
        </action>
    </package>

5、src下面添加包com.hhj.test.StrutsAction

內容如下:

public class StrutsAction {

	private String userName;
	private String userPswd;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPswd() {
		return userPswd;
	}

	public void setUserPswd(String userPswd) {
		this.userPswd = userPswd;
	}

	public String test() {
		System.out.println(userName);
		System.out.println(userPswd);

		return "success";
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章