Spring MVC 404在某些请求上出错

时间:2012-08-29 22:20:43

标签: spring java-ee web-applications spring-mvc

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

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

我想实施UserAccount&布线前的注册用例&迭代与弹簧安全。

但是我在表单提交上的注册表单(Register.jsp)(或action =“UserRegisteration / RegisterForm”method =“POST”>)给出404错误而不是提供视图(用于测试我有UserRegisterationController服务注册。 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 relevat snippet(在我的例子中是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表格sumbmit的结果:

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

HTTP状态404 - / UserRegisteration / RegisterForm


输入状态报告

message / UserRegisteration / RegisterForm

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


Apache Tomcat / 7.0.22


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

1 个答案:

答案 0 :(得分:0)

  

Register.jsp表格sumbmit的结果:   浏览器URL:(http:// localhost:8084 / UserRegisteration / RegisterForm)

     

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

这是正确的,缺少上下文根“/ cmgr”。向我们展示你的观点,大概是形式行动就是问题所在。

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

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

<form action="${targetURL}" [...] >
</form>`