• Struts2学习七----------Struts2后缀


    © 版权声明:本文为博主原创文章,转载请注明出处

    Struts2后缀

      - Struts2默认后缀是action

      - Struts2使用默认后缀时*.action和*都是同一个请求

      - Struts2自定义后缀后只能使用自定义的后缀访问(eg.*.c和*不再是同一个请求)

      - Struts2自定义后缀方式

        - 方式一:在struts.xml中配置struts.action.extension常量

        - 方式二:在web.xml中配置struts.action.extension

        - 方式三:在struts.properties中配置struts.action.extension

    实例

    1.项目结构

    2.pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    	
    	<modelVersion>4.0.0</modelVersion>
    	
    	<groupId>org.struts</groupId>
    	<artifactId>Struts2-CustomSuffix</artifactId>
    	<packaging>war</packaging>
    	<version>0.0.1-SNAPSHOT</version>
    	<name>Struts2-CustomSuffix Maven Webapp</name>
    	<url>http://maven.apache.org</url>
    	
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<struts.version>2.5.10</struts.version>
    	</properties>
    	
    	<dependencies>
    		<!-- junit -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.12</version>
    			<scope>test</scope>
    		</dependency>
    		<!-- struts2 -->
    		<dependency>
    		    <groupId>org.apache.struts</groupId>
    		    <artifactId>struts2-core</artifactId>
    		    <version>${struts.version}</version>
    		</dependency>
    	</dependencies>
    	
    	<build>
    	  <finalName>Struts2-CustomSuffix</finalName>
    	</build>
    	
    </project>
    

    3.web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      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"
      version="3.0" metadata-complete="true">
    
    	<!-- struts2过滤器 -->
    	<filter>
    		<filter-name>struts</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    		<!-- 方式二 -->
    		<!-- <init-param>
    			<param-name>struts.action.extension</param-name>
    			<param-value>cc</param-value>
    		</init-param> -->
    	</filter>
    	<filter-mapping>
    		<filter-name>struts</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
     
    </web-app>
    

    4.HelloAction.java

    package org.struts.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloAction extends ActionSupport {
    
    	private static final long serialVersionUID = 1L;
    
    	@Override
    	public String execute() throws Exception {
    		
    		return SUCCESS;
    		
    	}
    	
    }
    

    5.struts.xml

    <?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>
    
    	<package name="hello" extends="struts-default" namespace="/">
    		<action name="hello" class="org.struts.action.HelloAction">
    			<result>/index.jsp</result>
    		</action>
    	</package>
    	
    	<!-- 方式一 -->
    	<constant name="struts.action.extension" value="c"/>
    
    </struts>
    

    6.struts.properties

    #指定action后缀(方式三)
    #struts.action.extension=ccc
    

    7.index.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>index.jsp</title>
    </head>
    <body>
    	访问成功了。。。
    </body>
    </html>
    

    8.效果预览

      8.1 方式一

      

      8.2 方式二

      

      8.3 方式三

      

    参考:http://www.imooc.com/video/9002

  • 相关阅读:
    Beta 冲刺(5/7)
    Beta 冲刺(4/7)
    Beta 冲刺(3/7)
    Beta 冲刺(2/7)
    Beta 冲刺(1/7)
    福大软工 · 第十次作业
    PTA-B 1039 到底买不买 解题思路记录
    SDN负载均衡
    团队作业——项目验收与总结博客
    个人作业——软件工程实践总结作业
  • 原文地址:https://www.cnblogs.com/jinjiyese153/p/6874248.html
Copyright © 2020-2023  润新知