struts2入门helloword

这篇博客介绍了如何入门Struts2框架,包括添加必要的jar包、配置web.xml过滤器、创建index.jsp和suc.jsp页面、编写简单类以及配置struts.xml。文章详细解释了struts.xml中package、namespace、extends、action等关键配置项的作用,并展示了运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一是入门级别helloword

1、添加struts必须的一些的jar包

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
ognl-3.0.6.jar
struts2-core-2.3.24.jar
xwork-core-2.3.24.jar

2、在web.xml中配置过滤器

  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

3、接下来我们在webcontent下建立一个index.jsp,先简单的入门

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="hellostruts">hellostruts</a>
</body>
</html>

4、我们在web-info下建立suc.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
success!
</body>
</html>

5、接着建立一个简单的class

package com.slh.struts.demo;

public class Hello {
	public String hello(){
		return "success";
	}
}

6、我们在src下建立struts.xml

<?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="hellostruts" namespace="/" extends="struts-default">
	<action name="hellostruts" class="com.slh.struts.demo.Hello" method="hello">
		<result name="success">/WEB-INF/suc.jsp</result>
	</action>
</package>

</struts>

这个地方要说一下,package是模块,name随便起,主要是为了区分,namespace是命名空间,为了避免冲突。extends是继承。这里继承了struts-default;action的name就是你请求的名字,这里要与index一致,class就是Hello,方法呢就一个hello(),返回结果success,然后转发到/WEB-INF/suc.jsp,这里其实应该还有一个type属性来说明是转发(dispatcher)还是重定向(redirect)或者是其他情况。
运行结果如图:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值