實現第一個Struts2應用

一、概述html

 

 二、Struts2開發環境搭建java

根據事例選擇jar包。web

 操做小技巧:apache

將上面jar包統一打成User Library服務器

 

 

 小結:app

 三、helloStruts第一個應用程序jsp

建立helloStruts.jspide

<%@ 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>Insert title here</title>
</head>
<body>
hello struts!
</body>
</html>

修改struts.xmlui

<?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>
    <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
    <include file="example.xml"/> -->
    <!-- Add packages here -->
 <package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result >
                /helloStruts.jsp
            </result>
        </action>
    </package>
</struts>

注:註釋快捷鍵(ctrl+shift+/)取消(ctrl+shift+\)刪除(ctrl+d)spa

result:"/"指的是"WebRoot"

namespace:"/"指的是「http://localhost:8181/MyFirstStruts/hello」中「hello」前的「/」

 異常處理:

解釋:開發環境(Eclipse indigo版本)不支持將打好的

jar包加入到項目的部署環境中,也就是說,部署時有jar包,但jar包並無對項目提供支持。辦法:

 第二個異常:

解釋:沒有這個action(「/」),配置文件中的action爲「/hello」

辦法:

 嘗試:

 

 

 四、建立Action

 普通Action類:

package com.ljb.web.action;
public class HelloAction1 {
 public String execute(){
  return "success";
 }
}
<?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>
      <!-- Add packages here -->
    <constant name="struts.devMode" value="true" />
 <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.ljb.web.action.HelloAction1">
            <result>
                /helloStruts.jsp
            </result>
        </action>
    </package>
</struts>

<constant name="struts.devMode" value="true" />
這句話的做用:不用更改配置頻繁啓動服務器

實現Action接口類:

package com.ljb.web.action;
import com.opensymphony.xwork2.Action;
public class HelloAction2 implements Action {
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  return "success";
 }
}
 <?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>
      <!-- Add packages here -->
    <constant name="struts.devMode" value="true" />
 <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.ljb.web.action.HelloAction2">
            <result>
                /helloStruts.jsp
            </result>
        </action>
    </package>
</struts>

繼承ActionSupport類:

package com.ljb.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction3 extends ActionSupport {
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  return "success";
 }
}
 <?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>
      <!-- Add packages here -->
    <constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="com.ljb.web.action.HelloAction3">
            <result>
                /helloStruts.jsp
            </result>
        </action>
    </package>
</struts>

執行結果:

 

 

 五、關聯源碼

 

 

 

 

六、小結

相關文章
相關標籤/搜索