struts2入门

  1.下载struts2的jar包

下载地址:https://struts.apache.org/download.cgi#struts2514.1

我下载了的版本是 struts-2.3.34-all.zip

2.创建一个Project

3.导入struts2所需要的包

这个是重点,我在这里就吃了大亏。解压刚才下载的压缩包struts-2.3.34-all.zip,在apps文件夹下有个struts2-blank.war包,打开它,到WEB-INF/lib目录下,如下图所示,即为所需的最小包。包含的包应该和具体的Struts版本有关。必须根据自己的下载的包找,否则可能出现404,500等问题,也不要一个一个包复制到lib文件夹。

4.配置web.xml和struts.xml文件

web.xml

<?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_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>StrutsDemo</display-name>
    <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>
    
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list> 
</web-app>
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>
	<!-- 配置包,包名为default,该包继承了Struts2框架的默认包struts-default -->
    <package name="default" namespace="/" extends="struts-default">
    <!-- 定义名为hello的Action,该Action的处理类为com.action.TestAction,并映射到success.jsp -->
        <action name="hello" class="com.action.TestAction">
        <result>/success.jsp</result>
        </action>
    </package>
</struts>
另外要注意放的位置,struts2的jar包直接放在WEB-INF/lib文件夹下,web.xml放在WebContent下,struts.xml放在Src文件夹下。

5.前端页面index.jsp和success.jsp

index.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<s:a action="hello" >hello</s:a>
  
</body>
</html>

success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<s:property value="helo"></s:property>
</body>
</html>

6.开发后台Struts处理程序TestAction.java

在StrutsDemo工程下的src节点下创建包com.action,然后在该包下面创建TestAction.java文件。

TestAction.java代码如下

package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
	private static final long serialVersionUID=1L;
	//Action属性
	private String helo;
	//getter方法
	public String getHelo() {
		return helo;
	}
	public void setHelo(String helo) {
		this.helo = helo;
	}
	//重载execute()方法
	public String execute()throws Exception{
		helo="hello,world";
		return SUCCESS;
	}
}
项目中的文件如下图:有个小红叉,但是真心没问题。



猜你喜欢

转载自blog.csdn.net/pan_xi_yi/article/details/78977127
今日推荐