一、.struts2简单入门:
1.创建动态web工程(在创建是可以选择创建web.xml文件):
2.添加struts2的jar包(不用把lib目录下的jar全部导入,导入以下几个就好):
3.创建测试类:ActionTest 继承ActionSupport类,并重写execute方法:
package com.lijiancheng.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("all")
public class ActionTest extends ActionSupport {
public String execute() {
return "sucess";
}
}
4.配置action的访问路径:
4.1引入dtd约束文件(该dtd文件由struts-core-2.5.18.jar解压得到,并添加到xml catalog):
4.2在src目录下创建struts2的核心配置文件:struts.xml(使用事先引入的约束创建),并配置struts2:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.5.dtd" >
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="hello" extends = "struts-default" namespace="/"
strict-method-invocation="false">
<global-allowed-methods>regex:.*</global-allowed-methods>
<!-- name:为访问的名称 -->
<action name="action" class="com.lijiancheng.action.ActionTest" method="execute">
<!-- name:为返回的值, -->
<result name="sucess" >hello.jsp</result>
</action>
</package>
</struts>
5.在web.xml文件下配置struts2的过滤器:
<!-- 配置struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6.在webContent目录下创建用于访问的jsp文件(hello.jsp):
<%@ 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>
<h1>欢迎使用struts2...</h1>
</body>
</html>