Form form submission under struts 2, action needs to add project name to solve the problem

Keywords: Struts JSP xml Apache

Form form submission under struts 2, action needs to add project name to solve the problem

This is a test form for me to test whether struts 2 works normally.

index.jsp:

<form action="/loginManage" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="submit">
</form>

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="user" namespace="/"  extends="struts-default">
            <action name="loginManage" class="action.Login" method="success">
                <result name="success">/welcome.jsp</result>
                <result name="filed">/filed.jsp</result>
            </action>
        </package>
    </struts>

Login

public class Login extends ActionSupport {
    public String success(){
        HttpServletRequest request=ServletActionContext.getRequest();
        String username=request.getParameter("username");
        if("admin".equals(username)){
            return SUCCESS;
        }
        return "filed";
    }
}

Operation service
Well, no problem, then click Submit

-----wtf???
Look carefully, he actually removed my project name after submitting it, so I can't visit the background. I'll go back to check the code. I can't find out what's wrong. Then I went to the Internet and found that the problem is action.

When submitting, if "/" is added before the action, the absolute path is used by default. Then the project name must be added manually before the requested url, as follows:

<form action="u2flibserver_war_exploded/loginManage" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="submit">
</form>

Or add <% = request. Getcontextpath()% >

<form action="<%=request.getContextPath()%>/loginManage" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="submit">
</form>

.
.
.
.
Is it a bit troublesome? I think so I chose to remove "/" and use relative path.

<form action="loginManage" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="submit">
</form>



Success!

A small trouble, record it, convenient for future reference.

Posted by cetaces on Thu, 31 Oct 2019 22:52:54 -0700