Ubuntu开发Struts2应用(2、第一个程序)

本节介绍在ubuntu上开发的一个struts2应用。

新建一个web项目,利用myeclipse9.1给我们提供的struts2支持,搭建环境。

这里注意不要勾选多余的jar包,我们只需要core
myeclipse9.1集成的Sturts版本是struts2.2.1

代码如下:

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

</web-app>
struts.xml
<?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>
<package name="ubuntu" namespace="/linuxsight" extends="struts-default">
<action name="hello" method="execute" >
<result name="success">/WEB-INF/jsp/hello.jsp</result>
</action>
</package>
</struts>

HelloAction.java

package com.linuxsight.action;

public class HelloAction {
  private String msg;

  public String getMessage() {
   return msg;
  }

  public String execute(){
   msg = "在Ubuntu上开发的struts2应用|Linux视野";
   return "success";
  }

}

在/WEB-INF/jsp 新建hello.jsp
<h3>${message}</h3>

http://localhost:8080/struts2/linuxsight/hello

当然你也可以用最新的struts2.3,把相应的包拷贝到工作区lib里

使用struts2.3.1所需的JAR包
commons-fileupload-1.2.2.jar
commons-logging-1.1.1.jar
ognl-3.0.3.jar
xwork-core-2.3.1.jar
struts2-core-2.3.1.jar
freemarker-2.3.18.jar
commons-lang-2.5.jar
commons-io-2.0.1.jar
javassist-3.11.0.GA.jar
缺少后面三个包会报错:
严重: Exception starting filter struts2
Class: java.net.PlainSocketImpl
File: PlainSocketImpl.java
Method: connect
Line: 177 - java/net/PlainSocketImpl.java:177:-1

java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

这和使用struts2.1的时候有所不同

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: