简体   繁体   中英

How to navigate between html & jsp pages in spring

文件夹结构 I have a Html page called kind.html inside the WEB-INF directory and an other jsp page called registration.jsp inside the WEB-INF folder. I need to put this registration.jsp page inside the WEB-INF directory so it cannot be accessible if a user attempts to get access to it by typing its URL. So my problem is how can i navigate from kind.html to registration.jsp with link called home I am newbie in this Thank you. below is my code snippet and png file

kind.html..............................................

<li class='active'><a href='kind.html'><span>Home</span></a></li>
<li class='has-sub'><a href="registration.jsp"><span>Register</span></a>


.............................registrationcontroller........................................
@RequestMapping(value="/registration",method = RequestMethod.POST)
    public @ResponseBody
    String firstRegistration(HttpServletRequest req,
            HttpServletResponse response) {
        response.setContentType("text/html");

        RegistrationModel registrationModel = new RegistrationModel();
        registrationModel.setFirstName(req.getParameter("first_name"));
        System.out.println("controller " + req.getParameter("first_name") );
        registrationModel.setLastName(req.getParameter("last_name"));
        registrationModel.setPassword(req.getParameter("password"));
        registrationModel.setEmailID(req.getParameter("email"));
        System.out.println("controller email " + req.getParameter("email"));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
        try {
            Date date = format.parse(req.getParameter("BirthDate"));
            registrationModel.setDOB(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phoneno=req.getParameter("phoneNo");
        Integer phoneNo = Integer.parseInt(phoneno);
        System.out.println("phone no ...."+phoneNo);
        registrationModel.setPhoneNo(phoneNo);
        registrationModel.setGender(req.getParameter("gender"));
        String age=req.getParameter("Age");
        Long AGE = Long.parseLong(age);
        registrationModel.setAge(AGE);
        registrationModel.setAvtar(req.getParameter("Avtar"));
        System.out.println("avtar"+ req.getParameter("Avtar"));

        Address address = new Address();
        address.setAddressline(req.getParameter("Full-Address"));
        address.setCity(req.getParameter("city"));
        address.setLandmark(req.getParameter("landmark"));
        address.setState(req.getParameter("state"));
        String zipCode =req.getParameter("Zipcode");
        Long zipcode = Long.parseLong(zipCode);
        address.setZipcode(zipcode);
        registrationModel.setAddress(address);

        registrationService.resgistration(registrationModel);

        return "registration.jsp";

    }

..........................registration.jsp.........................................................
<form action="registration" method="post">
                        <fieldset>
                            <legend>Register Form</legend>
                            <div>
                                <input type="text" name="first_name" placeholder="First Name" />
                            </div>
                            <div>
                                <input type="text" name="last_name" placeholder="Last Name" />
                            </div>
                            <div>
                                <input type="password" name="password" placeholder="Password" />
                            </div>
                            <div>
                                <input type="text" name="email" placeholder="Email" />
                            </div>
                            <div>
                                <input type="text" name="BirthDate" placeholder="BirthDate" />
                            </div>
                            <div>
                                <input type="number" name="Age" placeholder="Age" />
                            </div>
                            <div>
                                <select name="gender">
                                    <option value="select">i am..</option>
                                    <option value="m">Male</option>
                                    <option value="f">Female</option>
                                </select><br> <br>

                            </div>
                            <div>
                                <input type="number" name="phoneNo" placeholder="PhoneNo" />
                            </div>
                            <div>
                                <input type="text" name="Full-Address"
                                    placeholder="Full-Address" />
                            </div>
                            <div>
                                <input type="text" name="landmark" placeholder="landmark" />
                            </div>
                            <div>
                                <input type="text" name="city" placeholder="city" />
                            </div>
                            <div>
                                <input type="text" name="state" placeholder="state" />
                            </div>
                            <div>
                                <input type="number" name="Zipcode" placeholder="Zipcode" />
                            </div>
                            <div>
                                <input type="file" name="Avtar" placeholder="Avtar" />
                            </div>
                            <input type="submit" name="submit" value="Send" />
                        </fieldset>
                    </form>![folder structure of project][2]

1st to call your pages html is static so you can use mvc resources "" for it, for jsp you have to use ViewResolver for jstl use InternalResourceViewResolver in its configuration as :

<bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/view/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
</bean>

then in your controller use

return "registration";

also you will need method to handle GET request and change below line

<li class='has-sub'><a href="registration"><span>Register</span></a>

if you are using Spring boot you will find these configuration automated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM