手动配置Struct2

1, 文件目录结构

apache-tomcat-7.0.65\webapps\structsHello\HelloWorld.jsp
apache-tomcat-7.0.65\webapps\structsHello\index.jsp
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\web.xml
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes\bf
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes\struts.xml
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes\bf\action
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes\bf\model
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes\bf\action\HelloWorldAction.class
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\classes\bf\model\MessageStore.class
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\commons-fileupload-1.3.1.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\commons-io-2.2.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\commons-lang3-3.2.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\commons-logging-api-1.1.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\freemarker-2.3.22.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\javassist-3.11.0.GA.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\ognl-3.0.6.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\struts2-core-2.3.24.1.jar
apache-tomcat-7.0.65\webapps\structsHello\WEB-INF\lib\xwork-core-2.3.24.1.jar

2,web.xml

<?xml version="1.0" encoding="UTF-8"?>
  
<web-app 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_3_0.xsd"
  version="3.0">

 <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>
  
</web-app>

3,struct.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>
 
  <constant name="struts.devMode" value="true" />
 
  <package name="basicstruts2" extends="struts-default">
    <action name="index">
      <result>/index.jsp</result>
    </action>
         
    <action name="hello" class="bf.action.HelloWorldAction" method="execute">
      <result name="success">/HelloWorld.jsp</result>
    </action>
  </package>
</struts>

4,index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>

5, HelloWord.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
    <title>Hello World!</title>
  </head>
 
  <body>
    <h2><s:property value="messageStore.message" /></h2>
  </body>
</html>

6, MessageStore.java

package bf.model;
 
public class MessageStore {
     
    private String message;
     
    public MessageStore() {
         
        setMessage("Hello Struts User");
    }
 
    public String getMessage() {
 
        return message;
    }
 
    public void setMessage(String message) {
 
        this.message = message;
    }
 
}

7, HelloWorldAction.java

package bf.action;

import bf.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;
 
public class HelloWorldAction extends ActionSupport {
 
    private static final long serialVersionUID = 1L;
 
    private MessageStore messageStore;
     
    public String execute() throws Exception {
         
        messageStore = new MessageStore() ;
        return SUCCESS;
    }
 
    public MessageStore getMessageStore() {
        return messageStore;
    }
 
    public void setMessageStore(MessageStore messageStore) {
        this.messageStore = messageStore;
    }
 
}

8,Java源码目录结构为:

E:\Hello\src\bf
E:\Hello\src\bf\action
E:\Hello\src\bf\model
E:\Hello\src\bf\action\HelloWorldAction.java
E:\Hello\src\bf\model\MessageStore.java

 编译时指定输入目录为E:\Hello\classes, 然后把编译的class文件手动复制到tomcat上面的目录结构中

 javac -c MessageStore.java -d E:\Hello\classes

 

参考文章:https://struts.apache.org/docs/hello-world-using-struts-2.html

https://files.cnblogs.com/files/season2009/structsHello.rar

posted @ 2015-12-23 18:10  Season2009  阅读(133)  评论(0编辑  收藏  举报