众所周知,开发技术的学习,都是从Hello World 开始的,我们学习Struts,也是从这开始!
开始我们先用3种方式实现Hello World!!!
(1)属性驱动
Action中设置属性,并提供get/set方法
先配置一下Struts的Web.xml(笔者用的是Struts2.5,2.5与之前的版本还是有点不同的)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>20180925_wfs_HelloWorld</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Action.java
public class HelloWorldActionTwo implements Action {
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devMode" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<global-allowed-methods>add, update</global-allowed-methods>
<action name="hello" class="com.hello.action.HelloWorldAction">
<result name="success" type="dispatcher">
/HelloWorldone.jsp
</result>
</action>
<action name="hello2" class="com.hello.action.HelloWorldActionTwo">
<result name="success" type="dispatcher">
/HelloWorldone.jsp
</result>
</action>
<action name="hello3" class="com.hello.action.HelloWorldActionThree">
<result name="success" type="dispatcher">
/HelloWorldThree.jsp
</result>
</action>
</package>
</struts>