0
votes

I’m developing Spring MVC, on Apache 7.xx application and have setup everything without any errors.

I have a my application map my dispatcher servlet to HomeController , which serves the view “home/View” which is also working.

I want to implement UserAccount & Registeration use case before wiring & itegrating with spring security.

However my registration form (Register.jsp) on form submit ( or action="UserRegisteration/RegisterForm" method="POST">) gives 404 error rather than serving a view via (for testing I have UserRegisterationController serve Register.jsp again)


Register.jsp (form snippet)

<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>

I have the following method createUserAccountRegisteration()mapped to

@RequestMapping(value="/RegisterForm", method=RequestMethod.POST) see below:

(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 (in my case 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 (relevant snippet)

 <!-- 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>

Result of Register.jsp form sumbmit:

Browser URL: (http://localhost:8084/UserRegisteration/RegisterForm)

HTTP Status 404 - /UserRegisteration/RegisterForm


type Status report

message /UserRegisteration/RegisterForm

description The requested resource (/UserRegisteration/RegisterForm) is not available.


Apache Tomcat/7.0.22


What i noticed is the browser url is missing my applications context (/cmgr) the correct url should be "http://localhost:8084/cmgr/UserRegisteration/RegisterForm"

1

1 Answers

0
votes

Result of Register.jsp form sumbmit: Browser URL: (http://localhost:8084/UserRegisteration/RegisterForm)

What i noticed is the browser url is missing my applications context (/cmgr) the correct url >should be "http://localhost:8084/cmgr/UserRegisteration/RegisterForm"

This is right, the context root "/cmgr" is missing. Show us your view, presumably the form action is the problem.

You could use the Spring url tag, like this:

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

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