笔记—初次使用Struts2.5.17框架

这篇博客介绍了如何使用Struts2.5.17框架进行初步搭建。首先从官网下载最新版Struts并解压,接着在Web工程中导入必要库。然后创建Action类、视图JSP文件,配置Struts.xml以启用开发模式。此外,还详细说明了URL映射、web.xml过滤器配置的步骤。
摘要由CSDN通过智能技术生成

这个版本是目前(2018)最新版,先到官网

 点击Full Distribution下载,然后解压到自己想要的一个目录下,:接着可以下载一个min版本(截图第三个)的,这个里面有我们需要的的基本包。

第一步:创建一个WEB动态工程。

第二步:导包,把刚刚下载的基本包导入到WE-BINF的lib目录里

第三步:创建一个action 类

first.java

package mypackage;

import com.opensymphony.xwork2.ActionSupport;

public class first extends ActionSupport {

		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		public String execute() throws Exception{
			System.out.println("执行Action");
			return SUCCESS;
		}

}

第四步:创建视图  first.jsp

<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" import = "javax.*"%>
<body>
	第一个Struts2程序
</body>

第五步:配置Struts.xml文件,该文件放在src下即可

<?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>

	<constant name="struts.devMode" value="true" />
	
	<package name = "default" namespace = "/" extends = "struts-default">	
			<action name="first"  class = "mypackage.first" >
				<result>/first.jsp</result>
			</action>				
	</package>	
</struts>

注意这个版本下一定要添加             <constant name="struts.devMode" value="true" />

第六步:创建URL  action

index.jsp

<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" import = "javax.*"%>
<html>
<head><title>first Struts</title></head>
	<body>
	<form action="first" method="POST">
		<input type = "submit" value = "登录">
	</form>
		
	</body>
</html>

第七步:配置web.xml文件

利用eclipse创建的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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>Struts2</display-name>
  

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

我们添加一个过滤器,结果如下

<?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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>Struts2</display-name>
  <filter>
	<filter-name>Struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
	<filter-name>Struts2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

<?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> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="helloworld" class="com.mytest.HelloWorldAction"> <result> /result.jsp </result> </action> </package> <package name="LoginForm" extends="struts-default"> <action name="login" class="com.mytest.LoginAction" method="execute"> <result> /login.jsp </result> </action> </package> </struts> <!--1.使用 struts2.5.16 版本 2.lib 文件夹下放置:工程所需jar包 3.xml标签库为远程获取,路径:http://struts.apache.org/dtds/struts-2.5.dtd 可设置为本地【xml输入语法快捷提示】,就不用远程获取了:window-->preference-->输入Catalog-->xml下的Catalog-->Add-->location:解压缩struts-core-2.5.16.jar 后,文件struts-2.5.dtd文件路径。 4.设置开发者模式: <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8" /> 每次HTTP请求系统都重新加载资源文件,有助于开发 5.struts配置文件改动后,是否重新加载 <constant name="struts.configuration.xml.reload" value="true" /> 6.查看源码:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->java Source Attachment-->External location :源码路径 7.查看文档API:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->javadoc location :输入网址 或选择源码DOC目录 8.拦截器:web.xml 配置拦截器<filter> struts2.5的filter-class 与struts2.5以前版本有所不同 <!-- 浏览器访问 http://localhost:8080/MyWeb/helloworld --> --> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置核心拦截器 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 struts2.5 --> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <!-- 拦截所有的url --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值