手動配置Struts2

1、前言

    經過手動配置Struts項目,能夠更好的瞭解Struts的運行原理。
css

2、資源

(一)Struts2。

3、步驟

(一)在MyEclipse中新建web項目MyProject

(二)將Struts2資源中的struts-blank項目相關文件部署到MyProject

1.部署web.xml

    將struts-blank項目的web.xml文件直接複製到MyProject的WEB-INF目錄下。
html

2.部署struts2.xml

    將struts-blank項目的web.xml文件直接複製到MyProject的src目錄下。而後將struts.xml中的<struts>標籤包含的內容清除。java

(三)在WebRoot目錄下編輯Login.jsp和result.jsp

1.Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.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="styles.css">
    -->

  </head>
  
  <body>
  <center>
      <form action="Login.action" method="post">
          UserName:<input name="username" type="text"><br>
          Password:<input name="password" type="password"><br>
          
          <input type="submit" value="sumit">
      </form>
  </center>
  </body>
</html>

2.result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'result.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="styles.css">
    -->

  </head>
  
  <body>
    hello
    
  </body>
</html>

(四)編輯Action,action.LoginAction.java

    類繼承了ActionSupport。
web

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

    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 {
        // TODO Auto-generated method stub
        return "success";
    }
    
    
}

(五)在struts.xml中配置Action

<?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> <!-- 原struts-blank項目中的包含的內容已刪除 -->
    <package name="struts2" extends="struts-default">
        <action name="Login" class="action.LoginAction">
            <result name="success">/result.jsp</result>
        </action>
    </package>
</struts>

(六)web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <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>


4、測試

(一)成功用例

    在瀏覽器訪問Login.jsp,完成信息輸入後返回result.jsp。
apache

相關文章
相關標籤/搜索