第一次使用struts2

在pom.xml中导入struts2-core-2.3.24.jar包

    	<dependency>
    		<groupId>org.apache.struts</groupId>
    		<artifactId>struts2-core</artifactId>
    		<version>2.3.24</version>
    	</dependency>

可以在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>

添加 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>
	<!-- 配置常量 -->
	<!-- 字符集 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<!-- 开发模式 -->
	<constant name="struts.devMode" value="true"></constant>
	<!-- 主题 -->
	<constant name="struts.ui.theme" value="simple"></constant>
	<!-- 扩展名 必须以action结尾,不要也可以删掉-->
	<constant name="struts.action.extension" value="action"></constant>

	<!-- 通用package -->
	<package name="customer" namespace="/" extends="struts-default">
				<!-- 客户端访问的地址			要访问Java文件的 包 的路径		访java文件中的哪一个方法 -->
		<action name="findById" class="crm.action.CustomerAction" method="findCustById">
			<!-- 方法返回的值给resut 判断和name的值是否一致,一致则执行-->
			<result name="success">/info.jsp</result>
		</action>

	</package>
</struts>

CustomerAction.java中的findCustById()方法

	public String findCustById() {
		System.out.println("传过来的客户ID是" + custId);
		return SUCCESS;
	}

浏览器中输入:
http://localhost:8080/maven-first/findById.action?custId=12

猜你喜欢

转载自blog.csdn.net/duanbaoke/article/details/86563859