標籤:blog http io ar os 使用 java sp for
本文轉自http://blog.csdn.net/liaisuo/article/details/9064527
今天在Eclipse搭建了Struts2 架構,並完成了一個很簡單的例子程式。
搭建好的全域圖如下:
第一步:在http://struts.apache.org/download.cgi下載Struts2的最新版即下載Full Distribution,這是Struts2的完整版
第二步:解壓下載的壓縮包。在struts-2.3.14.3 -> lib路徑下拷貝出所示的8個jar包,放入java工程下的lib目錄內。
第三步:在web.xml設定檔中,配置Struts2的核心Filter。代碼如下:
<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>
第四步:為了讓Struts運行起來,在src目錄下建立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="test" extends="struts-default">
<action name="login" class="com.rankexam.control.LoginAction">
<!-- 定義三個邏輯視圖和實體資源之間的映射 -->
<result name="input">/Login.jsp</result>
<result name="error">/Error.jsp</result>
<result name="success">/Welcome.jsp</result>
</action>
</package>
</struts>
第五步:建立處理使用者請求的Action類,該類繼承於ActionSupport類。代碼如下:
package com.rankexam.control;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction 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() throws Exception {
if(getUsername().equals("baidu") && getPassword().equals("baidu")){
ActionContext.getContext().getSession().put("user", getUsername());
return "success";
}else{
return "error";
}
}
}
第六步:完成Welcome、Error、Login頁面的編寫。代碼如下:
1.Login.jsp頁面代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="S" 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>登入頁面</title>
</head>
<body>
<S:form action="login" method="post">
<S:textfield name="username" label="使用者名稱"></S:textfield>
<S:password name="password" label="密碼"></S:password>
<S:submit text="登入"></S:submit>
</S:form>
</body>
</html>
2.Welcome.jsp頁面代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="S" 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>登入成功頁面</title>
</head>
<body>
${sessionScope.user}你好,您已經登入!
</body>
</html>
3.Error.jsp頁面代碼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>登入失敗頁面</title>
</head>
<body>
對不起,登入失敗!
</body>
</html>
經過上述流程,就完成了在Eclipse中搭建Struts2架構及其簡單的使用。
使用Eclipse搭建Struts2架構