关于struts-jquey-plugin中<sj:submit/>和<s:form/>的使用

本文介绍如何在Struts框架中使用AJAX实现动态表单提交,包括FormSubmission.jsp页面的构建,echoAction和simpleEchoAction的编写,以及对应的jsp页面展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一).FormSubmission.jsp页面:

<?xml version="1.0" encoding="utf-8"?>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Form 的使用</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="styles/layout.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/showcase.js"></script>
   <sj:head
   		locale="zh_CN"
    	loadAtOnce="true"
    	compressed="false"
    	jquerytheme="showcase"
    	customBasepath="themes"
    	loadFromGoogle="false"
    	debug="true"
    />
</head>
	<body>
		<strong>显示结果的DIV</strong>
	<div id="formResult" class="result ui-widget-content ui-corner-all"></div>
    
    <s:form id="form" action="echo" theme="simple" cssClass="yform">
        <fieldset>
            <legend>AJAX Form</legend>
	        <div class="type-text">
	            <label for="echo">Echo: </label>
	            <s:textfield id="echo" name="echo" value="Hello World!!!"/>
	        </div>
	        <div>
	            <sj:submit 
	            	id="formSubmit1"
	            	targets="formResult" 
	            	value="AJAX Submit" 
	            	indicator="indicator"
	            	button="true"
	            	/>

<!--内部定义了一个url和submit-->
 <s:url id="simpleecho" value="simpleEcho"/>
	            <sj:submit 
	            	id="formSubmit2"
	            	href="%{simpleecho}" 
	            	targets="formResult" 
	            	value="AJAX Submit 2" 
	            	indicator="indicator"
	            	button="true"
	            	/>
	        </div>
        </fieldset>
    </s:form>

    <img id="indicator" src="images/indicator.gif" alt="Loading..." style="display:none"/>    
    
	<div class="code ui-widget-content ui-corner-all">
	</body>
</html>

 (二)1.echo Action的写法:

package com.anchorajax;

import com.opensymphony.xwork2.ActionSupport;

public class Echo extends ActionSupport
{
    private String echo;
    private boolean escape = true;

    public boolean isEscape() {
        return escape;
    }

    public void setEscape(boolean escape) {
        this.escape = escape;
    }

    public String getEcho() {
        return echo;
    }

    public void setEcho(String echo) {
        this.echo = echo;
    }

    public String execute() throws Exception {

        return SUCCESS;
    }
}

 2.simpleEcho的Action写法:

package com.anchorajax;

import com.opensymphony.xwork2.ActionSupport;

public class SimpleEcho extends ActionSupport
{
    private String echo;
    private boolean escape = true;

    public boolean isEscape() {
        return escape;
    }

    public void setEscape(boolean escape) {
        this.escape = escape;
    }

    public String getEcho() {
        return echo;
    }

    public void setEcho(String echo) {
        this.echo = echo;
    }

    public String execute() throws Exception {

        return SUCCESS;
    }
}

 (三)struts.xml的配置:

 <!-- echoAction --> 		
   		<action name="echo" class="com.anchorajax.Echo">
   			<result name="success">/echo.jsp</result>
   		</action>
   		
   		<!-- SimpleEchoAction --> 	
   		<action name="simpleEcho" class="com.anchorajax.SimpleEcho">
   			<result name="success">/SimpleEcho.jsp</result>
   		</action>

 (四)1.echo.jsp页面:

<%@ taglib prefix="s" uri="/struts-tags"%>
<p>Echo : <s:property value="echo" escape="%{escape}"/></p>

      2.SimpleEcho.jsp页面:

<%@ taglib prefix="s" uri="/struts-tags"%>
<s:property value="echo" escape="%{escape}"/>

 (五)显示界面:


 


<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/xsd/maven-4.0.0.xsd"> <parent> <groupId>io.renren</groupId> <artifactId>renren-security</artifactId> <version>4.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>renren-api</artifactId> <packaging>jar</packaging> <description>renren-api</description> <dependencies> <dependency> <groupId>io.renren</groupId> <artifactId>renren-common</artifactId> <version>4.0.0</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 跳过单元测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.14</version> <configuration> <imageName>renren/api</imageName> <dockerDirectory>${project.basedir}</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> <!-- 运行命令 mvn clean package docker:build 打包并生成docker镜像 --> </plugin> </plugins> </build> </project> 这是完整的pom文件 请帮我修改一下
06-15
<think>我们面临的问题是在Maven的POM文件中,几个插件(spring-boot-maven-plugin,maven-surefire-plugin,docker-maven-plugin)出现了报红(通常是依赖无法解析或版本问题)。根据引用内容,我们可以采取以下步骤:1.**为插件指定版本**:很多情况下插件报红是因为没有指定版本,Maven无法从仓库中找到合适的版本。我们需要为每个插件指定一个明确的、可用的版本。2.**使用镜像仓库**:由于网络原因,可能无法从中央仓库下载插件,配置阿里云镜像可以加速下载(引用[3])。3.**更新过时的插件**:例如,引用[2]提到docker-maven-plugin,而这里使用的是Spotify的旧版本(0.4.14),该插件已被归档,建议使用fabric8的docker-maven-plugin。4.**清理IDE缓存**:有时候是IDE的缓存问题,需要清理并重新导入(引用[3]中提到)。具体步骤:###1.为插件指定版本-**spring-boot-maven-plugin**:这个插件的版本应该与SpringBoot的版本一致。例如,如果项目使用的是SpringBoot2.7.5,那么插件版本也应该是2.7.5。通常,在SpringBoot项目中,父POM已经管理了版本,但如果没有,需要显式指定。-**maven-surefire-plugin**:这是一个常用的测试插件,引用[1]中提到的问题是版本2.12.4,但我们可以使用更新的版本,比如3.0.0-M5或3.1.2(稳定版)。-**docker-maven-plugin**:引用[2]中提到了使用docker-maven-plugin构建镜像,但注意Spotify的插件已经不再维护,建议切换到fabric8的docker-maven-plugin,并指定较新版本(如0.42.1)。###2.配置阿里云镜像在Maven的settings.xml(用户目录下的.m2目录)中配置阿里云镜像,以加速下载。###3.修改POM文件根据以上分析,修改POM文件中的插件部分。###修改后的POM插件配置示例```xml<build><finalName>${project.artifactId}</finalName><plugins><!--SpringBootMavenPlugin--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!--请根据实际SpringBoot版本设置--><version>2.7.5</version></plugin><!--跳过单元测试--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.1.2</version><!--使用最新稳定版--><configuration><skipTests>true</skipTests></configuration></plugin><!--使用fabric8的docker-maven-plugin替代spotify的旧版--><plugin><groupId>io.fabric8</groupId><artifactId>docker-maven-plugin</artifactId><version>0.42.1</version><configuration><images><image><name>renren/api</name><build><dockerFileDir>${project.basedir}</dockerFileDir><assembly><descriptorRef>artifact</descriptorRef></assembly></build></image></images></configuration></plugin></plugins></build>```###4.执行Maven命令在修改POM后,运行以下命令更新依赖:```bashmvncleanpackage-U```###5.清理IDE缓存(以IntelliJ为例)-选择File->InvalidateCaches/Restart...-选择InvalidateandRestart###注意事项-**版本匹配**:确保所有插件的版本相互兼容,特别是SpringBoot相关插件。-**Docker构建**:fabric8的docker-maven-plugin配置方式与spotify有所不同,上面示例使用了`<dockerFileDir>`指定Dockerfile所在目录,并使用了`<assembly>`来包含构建的jar文件(需要调整Dockerfile中的路径,因为构建上下文会包含target目录下的jar文件)。-**Dockerfile**:确保项目根目录下有正确的Dockerfile,并且Dockerfile中引用的jar包路径与构建后的路径一致。###引用说明-引用[1]:解决surefire插件执行失败的问题,通过指定版本可以避免。-引用[2]:使用docker-maven-plugin构建镜像,但注意插件已更换为fabric8。-引用[3]:配置镜像仓库解决依赖下载问题。通过以上步骤,应该能够解决插件报红的问题,并正确配置这些插件。§§相关问题§§1.如何解决Maven依赖下载失败的问题?2.SpringBoot项目打包时`spring-boot-maven-plugin`有哪些高级配置?3.Maven多模块项目中如何统一管理插件版本?4.Docker镜像构建失败时如何进行调试?</think>以下是解决 Maven 插件报红问题的完整配置方案,结合引用内容最佳实践进行优化: ### 解决方案:修复插件配置报红问题 #### 1. **spring-boot-maven-plugin 配置优化** ```xml <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <!-- 与Spring Boot版本一致 --> <executions> <execution> <goals> <goal>repackage</goal> <!-- 生成可执行JAR --> </goals> </execution> </executions> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> ``` **关键点**: - 必须添加 `<version>` 标签(与项目中 Spring Boot 版本保持一致) - 使用 `${spring-boot.version}` 变量统一管理版本[^3] #### 2. **maven-surefire-plugin 修复方案** ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <!-- 推荐稳定版本 --> <configuration> <skipTests>true</skipTests> <!-- 解决常见测试失败问题 --> <argLine>-Dfile.encoding=UTF-8</argLine> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> ``` **修复说明**: - 添加版本号解决 `Cannot resolve plugin` 错误[^1] - 配置编码参数避免测试乱码失败 - `useSystemClassLoader` 解决类加载冲突 #### 3. **docker-maven-plugin 升级方案** ```xml <plugin> <groupId>io.fabric8</groupId> <!-- 替代已归档的spotify插件 --> <artifactId>docker-maven-plugin</artifactId> <version>0.42.1</version> <configuration> <images> <image> <name>renren/api:${project.version}</name> <build> <dockerFileDir>${project.basedir}</dockerFileDir> <assembly> <descriptorRef>artifact</descriptorRef> </assembly> </build> </image> </images> </configuration> </plugin> ``` **升级要点**: - Spotify 插件已停止维护,改用 Fabric8 官方推荐替代品[^2] - 使用 `<assembly>` 描述符简化资源映射 - 添加镜像标签 `${project.version}` 实现版本追踪 ### 完整配置示例 ```xml <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- Spring Boot 插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- 测试插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!-- Docker 插件 --> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.42.1</version> <configuration> <images> <image> <name>renren/api:${project.version}</name> <build> <dockerFileDir>${project.basedir}</dockerFileDir> <assembly> <descriptorRef>artifact</descriptorRef> </assembly> </build> </image> </images> </configuration> </plugin> </plugins> </build> ``` ### 验证步骤 1. **更新本地仓库**: ```bash mvn clean install -U -Dmaven.test.skip=true ``` 2. **构建 Docker 镜像**: ```bash mvn docker:build -DskipTests ``` 3. **检查镜像**: ```bash docker images | grep renren/api ``` ### 常见问题解决 | 错误类型 | 解决方案 | |---------|---------| | `Plugin not found` | 添加阿里云镜像仓库[^3] | | `Unknown lifecycle phase` | 检查 Maven 版本 ≥ 3.6.3 | | `Docker connection refused` | 配置 Docker 远程连接[^2] | > **重要提示**:在 `settings.xml` 中添加阿里云镜像可解决 90% 依赖下载问题: > ```xml > <mirror> > <id>aliyun</id> > <url>https://maven.aliyun.com/repository/public</url> > <mirrorOf>*</mirrorOf> > </mirror> > ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值