转: 一个完整的struts 2 Hello World程序(5)上
2.2??一个简单的HelloWorld
<?xml?version="1.0"?encoding="UTF-8"?>
<web-app?id="WebApp_ID"?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">
????<filter>
????????<!—?配置filter--?>
????????<filter-name>struts2</filter-name>
????????<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
????</filter>
????<filter-mapping>
????????????<!—拦截所有URL用户请求
????????<filter-name>struts2</filter-name>
????????<url-pattern>/*</url-pattern>
????</filter-mapping>
????<!—配置欢迎界面文件--?>
<welcome-file-list>
????????<welcome-file>index.html</welcome-file>
????????<welcome-file>index.htm</welcome-file>
????????<welcome-file>index.jsp</welcome-file>
????????<welcome-file>default.html</welcome-file>
????????<welcome-file>default.htm</welcome-file>
????????<welcome-file>default.jsp</welcome-file>
????</welcome-file-list>
</web-app>
<?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">
<!—配置struts2-->
<struts>
????<!--?配置包,名称为bookcode??-->
????<package?name="bookcode"?extends='struts-default'>
????<!--?配置Action??-->
????<action?name="HelloWorld"?class="ch2.helloworld.HelloWorld">
????????????<!--?配置返回结果??-->
????????????<result?name="success">/ch2/helloworld/success.jsp</result>
????????????<result?name="error">/ch2/helloworld/error.jsp</result>
????????</action>
????</package>
?
</struts>
package?ch2.helloworld;
public?class?HelloWorld?{
????//定义msg属性
????private?String?msg;
????//msg的get方法
????public?String?getMsg()?{
????????return?msg;
????}
????//msg的set方法
????public?void?setMsg(String?msg)?{
????????this.msg?=?msg;
????}
????//Action的execute()处理方法
????public?String?execute()?{
????????//判断条件
????????if?(getMsg().equals(""))?{
????????????//显示错误信息
????????????System.out.println("no?String?input!");
????????????//返回错误结果
????????????return?"error";
????????}?else?{
????????????//显示用户输入的信息
????????????System.out.println(getMsg());
????????????//返回一个处理成功结果
????????????return?"success";
????????}
????}
}
?