软件151 刘光星
一、下載Struts
建立web項目,給項目添加外部引用包(project-properties-Java Build Path-Add External Jars...)。添加的包有:commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,commons-logging-api-1.1.jar,freemarker-2.3.16.jar,javassist-3.7.ga.jar,ognl-3.0.jar,struts2-core-2.2.1.1.jar,xwork-core-2.2.1.1.jar。注意:由于struts2版本的差異性,上面提到的包不一定滿足所有版本的需求。配置完struts2后,請部署運行一下。根據運行時的錯誤提示來添加jar包解決問題。
二、創建web項目,導入使用struts2所必須的jar包。
在MyEclipse項目中的src根目錄下建立一個struts.xml文件。(可以打開下載的struts2安裝包里的apps目錄下的任意一個jar包,在里面的WEB_INFR/src目錄下,尋找struts.xml文件,將該文件復制進項目的src根目錄下,將里面的內容清空(只留下<struts>標簽和頭部標簽即可))。
四、在web.xml中加入struts2 MVC框架啟動配置
和struts.xml文件的生成類似,在struts2安裝包里找到web.xml文件,將里面的<filter>和<filter-mapping>標簽及其內容拷貝進項目中的web.config文件即可。
五、struts2實例--簡單的登錄例子
?????5.1 編寫login.jsp頁面。代碼如下:
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
????
??? <title>Login</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="/login" method="post">
? ??<s:label value="系統登陸"></s:label>
? ??<s:textfield name="username" label="賬號" />
? ??<s:password name="password" label="密碼" />
? ??<s:submit value="登錄" />
? ?</s:form>
? </body>
</html>
??? 5.2 編寫LoginAction類。代碼如下:
package com.gsww.kingreturns.struts2.excise;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {//該類繼承了ActionSupport類。這樣就可以直接使用SUCCESS, LOGIN等變量和重寫execute等方法
?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;
?}
?@Override
?public String execute() throws Exception {
??if("haha".equals(username) && "hehe".equals(password))//如果登錄的用戶名=haha并且密碼=hehe,就返回SUCCESS;否則,返回LOGIN
???return SUCCESS;
??return LOGIN;
?}
}
??? 5.3 配置struts.xml文件。代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
??? "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
??? "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
??? <package name="default" namespace="/" extends="struts-default">
??????? <action name="login" class="com.gsww.kingreturns.struts2.excise.LoginAction" method="execute">
??????????? <result name="success">/welcome.jsp</result>
??????????? <result name="login">/login.jsp</result>
??????? </action>
??? </package>
</struts>
5.4 配置web.xml文件。代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"?
?xmlns="http://java.sun.com/xml/ns/javaee"?
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?
?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?
?<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>
5.5 根據struts.xml里配置的內容,還需要一個welcome.jsp頁面。編寫welcome.jsp頁面。代碼如下:
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
????
??? <title>My JSP 'welcome.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>
? ?歡迎${username }!
? </body>
</html>
經過上述步驟,登錄實例已經編寫完畢。啟動tomcat,在網頁地址欄里輸入:http://localhost:8080/項目的名稱/login.jsp,打開登錄頁面。如下所示:
輸入用戶名:haha,密碼:hehe,點擊登錄,就來到了welcome.jsp頁面。如下所示:
如果輸入的用戶名和密碼不是haha和hehe,那么,就來到了login.jsp頁面。
轉載于:https://www.cnblogs.com/liuguangxing/p/7091837.html
總結
- 上一篇: 使用Filter跟踪Asp.net MV
- 下一篇: 15款 有用SDK