搭建struts2

新建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="default" namespace="/" extends="struts-default">
	      <!-- name访问名,class为类名, -->
		<action name="helloWorld" class="com.it.action.HelloAction">
		<!-- name为返回内容 -->	
			<result name="success">jsp/helloWorld.jsp</result>
		</action>
	</package>
</struts>

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>

事例 HelloAction

package com.it.action;

import com.opensymphony.xwork2.Action;

public class HelloAction implements Action{

	@Override
	public String execute() throws Exception {
		System.out.println("1111");
		//返回值和struts里面name对应
		return "success";
	}

}

``![记得加载JAR包](https://img-blog.csdnimg.cn/20181101152642507.png)`

猜你喜欢

转载自blog.csdn.net/weixin_42470710/article/details/83621226