实现第一个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>

执行结果:

 

 

 五、关联源码

 

 

 

 

六、小结

相关文章
相关标签/搜索