Struts2项目建立

1、在eclipse中创建web工程

2、导入jar包:WEB-INF/lib
下载地址Struts2jar包,选择min的就够了:
jar包种类

3、配置Struts2的前端控制器web.xml中

 <!-- 配置struts2前端控制器 -->
  <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>

4、编写业务处理类(src下 cn.sxt.action包下)

package cn.sxt.action;

public class HelloAction {
	/**
	 * 在Struts2中所有的业务处理方法都是public的
	 * 返回值都是String,都没有参数,方法名可以自定义默认是execute
	 * @return
	 */
	public String execute() {
		System.out.println("hello strut2");
		return "success";
	}

}

5、Struts2的配置文件src/struts.xml,文件名不能被更改,配置action

<?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="default" namespace="/" extends="struts-default">
	<!-- 配置URL和action -->
	<action name="hello" class="cn.sxt.action.HelloAction">
	<result>/hello.jsp</result>
	</action>
	</package>
</struts>

6、编写对应的jsp文件:hello.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>success</h1>
</body>
</html>

7、发布项目并测试

启动服务器,打开界面:http://localhost:8080/strutsDemo/hello

可以看到:一级标题:success

后台打印:hello strut2

简单配置讲解:

<!--package分模块管理,
name是自定义的,但是不能重复,在一个项目中唯一
namespace:命名空间,和URL请求路径相关,如果是/,那么就是/hello.jsp,如果是/user,那么就是/user/hello.jsp
extends:直接或者间接继承struts-default-->
<package name="default" namespace="/" extends="struts-default">
	<!-- action 配置URL和action处理的映射
	name是请求后缀,不需要加后缀
    class:处理类的完全限定名称,包名+类名,如果不配置由默认类进行处理(ActionSupport类)
    method:method="execute"配置执行的方法,默认是execute
-->
	<action name="hello" class="cn.sxt.action.HelloAction">
        <!--result:结果集配置
        name="":结果集名称和方法的返回值匹配,默认是success;提供了五个返回结果:
			ACTION.SUCCESS:执行成功,跳转到下个视图
			ACTION.NONE:执行成功,不需要视图展示
			ACTION.ERROR:执行失败,返回错误的页面
			ACTION.INPUT:想要执行该action需要更多的条件
            ACTION.LOGIN:需要登录后才能执行
       type="":指定响应结果的类型dispather:转发,默认
			redirect:重定向
			redirectAction:重定向到action
		值:为跳转页面,不加/为相对于namespace的相对路径,建议加
		-->
	<result>/hello.jsp</result>
	</action>
</package>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值