You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何避免Struts2页面加载时自动触发Action?新手开发求助

Fixing Struts2 Action Auto-Trigger & Initial Capacity Exception

Hey there, fellow Struts2 newbie! Let’s work through this frustrating issue together—your login Action firing automatically on page load, throwing that java.lang.IllegalArgumentException: Initial capacity must be at least one but was 0 error. I’ve dealt with similar headaches when I started out, so let’s break down the fixes step by step:

1. Check for <s:action> Tags in Your Login Page

This is the most common culprit for Actions running on page load. If you’re using Struts2’s <s:action> tag to reference your login Action in the landing page, it executes the Action’s execute() method by default when the page renders.

For example, if you have code like this:

<s:action name="loginAction" namespace="/" />

It will trigger the Action as soon as the page loads. Fix this by adding the executeResult="false" attribute—it tells Struts to instantiate the Action but skip running its execution method:

<s:action name="loginAction" namespace="/" executeResult="false" />

2. Audit Collection Initialization in Your LoginAction

The error message points directly to a collection (like HashMap, ArrayList) being initialized with a capacity of 0. Double-check your Action class for code like:

// Bad: Initial capacity 0 violates the collection's requirements
private Map<String, String> userCredentials = new HashMap<>(0);
private List<String> roles = new ArrayList<>(0);

Struts might also be injecting a 0 value into a collection property via configuration. Fix this by:

  • Using the no-arg constructor (which uses a safe default capacity, e.g., 16 for HashMap):
    private Map<String, String> userCredentials = new HashMap<>();
    
  • Or explicitly setting a capacity greater than 0:
    private List<String> roles = new ArrayList<>(5);
    

3. Verify Struts Configuration (struts.xml)

Make sure your login Action isn’t set as the default action for its package. If your package has a default-action-ref pointing to your login Action, it will trigger automatically when the root URL is accessed (your landing page):

<!-- Avoid this if your landing page is the root URL -->
<package name="default" namespace="/" extends="struts-default">
  <default-action-ref name="loginAction" />
  <action name="loginAction" class="com.yourpackage.LoginAction">
    <result name="success">/home.jsp</result>
    <result name="input">/login.jsp</result>
  </action>
</package>

Remove the default-action-ref line or set it to a neutral Action that doesn’t require execution on load.

4. Quick Check: Form Submission Trigger

While you said form submission works normally, double-check your login form’s submit button. Ensure it’s using type="submit" and there’s no accidental JavaScript that triggers form submission on page load (like a misplaced onload event).

Start with the <s:action> tag fix first—it’s the most likely cause, then move to the collection initialization if the error persists. You’ll have this sorted in no time!

内容的提问来源于stack exchange,提问作者WCM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:37:03