Struts2基础应用二

第一个Struts2应用:
1、新建一个Web项目,导入一些所需的jar包;

2、[b]进行Struts2的配置[/b],struts.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>
<package name="huhui" [b]namespace[/b]="/com/huhui" extends="struts-default">
<action [b]name[/b]="helloworld" class="com.huhui.action.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>

3、[b]配置web.xml[/b]代码:
<?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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>

4、[b]编写Action[/b],HellowWorldAction.java和hello.jsp代码:
public class HelloWorldAction {
private String message;
//省略get/set方法
public String execute() throws Exception{//方法
message="我的第一个struts2应用";
ActionContext.getContext().put("message", message);
return "success";
}

//public String message(){
// return "message";
//}
}


<body>
${message }
</body>


5、测试,第一个Struts2应用就完了。所需的jar包在附件中。最后说明一下:
测试时访问的路径跟你所设置的命名空间(namespace已加粗)和action中的name(已加粗)属性值有关,最后的.action后缀可加可不加。访问路径是:http://localhost:8080/项目名/命名空间属性值(我这里是/com/huhui)/action中的name值(我这里是helloworld).action
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值