1.添加一个web项目
2.修改web.xml
<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>
3.添加jar包
4.添加struts.xml配置文件
5.添加Action
6.
URL:http://IP:port/web应用的名字/namespace的值/action名字
package:
namespace:可以省略,等价于namespace="",所有路径上都可以访问action
我的第一个struts2的配置
<?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.1.dtd">
<struts>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package> -->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<package name="hello" namespace="/" extends="struts-default">
<action name="hello" class="com.neusoft.hello.HelloAction">
<result name="error">/hello.jsp</result>
<result name="add">/index.jsp</result>
</action>
</package>
</struts>
package com.neusoft.hello;
public class HelloAction {
public HelloAction()
{
System.out.println("action..");
}
public String execute()
{
System.out.println("hello");
int i=10;
if(i==10)
{
return "error";
}
else
{
return "add";}
}
}
在web根目录下创建两个hello.jsp和index.jsp
这样就减少了severlet和jsp之间频繁设置跳转语句的问题