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

这个版本是目前(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>

猜你喜欢

转载自blog.csdn.net/qq_41900081/article/details/82956075
今日推荐