0

我正在开发 Spring MVC,在 Apache 7.xx 应用程序上并设置了所有内容,没有任何错误。

我有一个我的应用程序将我的调度程序 servlet 映射到 HomeController,它为视图“home/View”提供服务,该视图也在工作。

我想在使用 Spring Security 进行布线和迭代之前实现 UserAccount 和注册用例。

但是,我在表单提交(或 action="UserRegisteration/RegisterForm" method="POST">)上的注册表单(Register.jsp)给出了 404 错误,而不是通过提供视图(为了测试,我让 UserRegisterationController 再次服务 Register.jsp)


Register.jsp(表单片段)

<form action="/UserRegisteration/RegisterForm" method="POST">
                <table width="283" border="1">
  <tr>
    <td width="123"><label for="firstname">First Name</label></td>
    <td width="144"><input type="text" name="firstname" id="firstname"></td>
  </tr>
  <tr>
    <td><label for="lastname">Last Name</label></td>
    <td><input type="text" name="lastname" id="lastname"></td>
  </tr>
  <tr>
    <td><label for="email">Email</label></td>
    <td><input type="text" name="email" id="email"></td>
  </tr>
  <tr>
    <td><label for="phonecontact1">Phone Contact 1</label></td>
    <td><input type="text" name="phonecontact1" id="phonecontact1"></td>
  </tr>
  <tr>
    <td><label for="phonecontact2">Phone Contact 2</label></td>
    <td><input type="text" name="phonecontact2" id="phonecontact2"></td>
  </tr>
  <tr>
    <td><label for="address1">Address 1</label></td>
    <td><input type="text" name="address1" id="address1"></td>
  </tr>
  <tr>
    <td><label for="address2">Address 2</label></td>
    <td><input type="text" name="address2" id="address2"></td>
  </tr>
  <tr>
    <td><label for="industry ">Industry </label></td>
    <td><input type="text" name="industry " id="industry "></td>
  </tr>
  <tr>
    <td><label for="password">Enter Desired Password</label></td>
    <td><input type="text" name="password" id="password"></td>
  </tr>
  <tr>
    <td><input type="reset" name="clear " id="clear " value="Clear Fields"></td>
    <td><input type="submit" name="register" id="register" value="Register"></td>
  </tr>
</table>
</form>

我有以下方法 createUserAccountRegisteration() 映射到

@RequestMapping(value="/RegisterForm", method=RequestMethod.POST) 见下文:

(UserRegisterationController.java)
@Controller
@RequestMapping("/UserRegisteration")
public class UserRegisterationController {
    @Autowired
    private RegisterationService registerationService;
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private PasswordService passwordService;
    public UserRegisterationController() {
    }
    public UserRegisterationController(RegisterationService registerationService, UserAccountService userAccountService, PasswordService password) {
        this.registerationService = registerationService;
        this.userAccountService = userAccountService;
        this.passwordService = password;
    }
    //if checked on register link , forward to registeration page
    @RequestMapping(value="/RegisterForm", method=RequestMethod.GET)
    public String serveRegisterationView()
    {
       return  "UserAccount/Register";
    }
    @RequestMapping(value="/RegisterForm", method=RequestMethod.POST)
    public String createUserAccountRegisteration()
    {
     //if submited registeration 
    //check for previous registeration 
    //if registered prompt , forward to sign in 
    //else create registeration , and user account , and password , forward to main user page  
       return  "UserAccount/Register";
    }

}

web.xml

<web-app metadata-complete="true" version="3.0" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext.xml
        /WEB-INF/hibernate-context.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>cmgr</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cmgr</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout> 
            30 
        </session-timeout>
    </session-config>
</web-app>

spring-servlet 相关片段(在我的例子中是 cmgr-servlet)

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"     
        p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView" />

applicationContext.xml(相关片段)

 <!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.   For example @Controller and @Service. Make sure to set the correct base-package--> 
    <context:component-scan base-package="com.cmgr.*" />    
    <!-- Configures the annotation-driven Spring MVC Controller programming model.  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
    <mvc:annotation-driven/>
    <!-- mapping of static resources-->
    <mvc:resources mapping="/resources/**" location="/resources/" />

</beans>

Register.jsp 表单提交的结果:

浏览器网址:(http://localhost:8084/UserRegisteration/RegisterForm)

HTTP 状态 404 - /UserRegisteration/RegisterForm


类型状态报告

消息 /UserRegisteration/RegisterForm

描述 请求的资源 (/UserRegisteration/RegisterForm) 不可用。


Apache Tomcat/7.0.22


我注意到浏览器 url 缺少我的应用程序上下文 (/cmgr) 正确的 url 应该是“http://localhost:8084/cmgr/UserRegisteration/RegisterForm”

4

1 回答 1

0

Register.jsp 表单提交结果:浏览器 URL:(http://localhost:8084/UserRegisteration/RegisterForm)

我注意到浏览器 url 缺少我的应用程序上下文 (/cmgr) 正确的 url >应该是“http://localhost:8084/cmgr/UserRegisteration/RegisterForm”

没错,缺少上下文根“/cmgr”。向我们展示您的观点,大概是表单动作是问题所在。

您可以使用 Spring url 标签,如下所示:

<spring:url value="/UserRegisteration/RegisterForm" var="targetURL"/>

<form action="${targetURL}" [...] >
</form>`
于 2012-08-29T23:16:18.930 回答