“HTTP状态404” - 无法使用spring MVC

时间:2016-01-05 07:50:14

标签: jsp spring-mvc

我正在使用Spring MVC开发简单的登录,注册实现。

我的目标网页是index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>
</head>
<body>
<center>
    Sign in to continue to Gmail </br> </br>
    <form method="post" action="Login/loginPage">
        <table>
            <tr>
                <td>User Name</td>
                <td><input type="text" name="userName" placeholder="Enter usename"
                    required></td>
            </tr>
            <tr></tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="pwd"
                    placeholder="Enter Password" required></td>
            </tr>
            <tr></tr>
            <tr>
                <td></td>
                <td><input type="submit" value="submit"></td>
            </tr>
        </table>
        <br>
        <font color="red">${msg}</font>
    </form>
    </br> </br><a href="register.jsp">Create account</a>
</center>
</body>
</html>

从着陆页,我想重定向到“register.jsp”

我收到以下错误。

HTTP状态404 - /My_Login/register.jsp

输入状态报告

消息/My_Login/register.jsp

说明请求的资源不可用。

Apache Tomcat / 8.0.28

其余代码如下

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
</head>
<body>
<center>
    Create your Google Account</br> </br>
    <form method="post" action="Login/registrationPage">
        <table>
            <tr>
                <td>Full Name</td>
                <td><input type="text" name="fullName" required></td>
            </tr>
            <tr></tr>
            <tr>
                <td>User Name</td>
                <td><input type="text" name="userName" required></td>
            </tr>
            <tr></tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="pwd" required></td>
            </tr>
            <tr></tr>
            <tr>
                <td>Street</td>
                <td><input type="text" name="address.street"></td>
            </tr>
            <tr></tr>
            <tr>
                <td>City</td>
                <td><input type="text" name="address.city"></td>
            </tr>
            <tr></tr>
            <tr>
                <td>Pincode</td>
                <td><input type="number" name="address.pincode"></td>
            </tr>
            <tr></tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Register"></td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>My_Login</display-name>
<welcome-file-list>
    <welcome-file>/view/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

APP-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>

xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:component-scan base-package="org.Naina.app" />
<context:annotation-config />

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

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/firstdb" />
    <property name="username" value="root" />
    <property name="password" value="Tiger" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>org.Naina.app.dto.Address</value>
            <value>org.Naina.app.dto.User</value>
        </list>
    </property>
</bean>

</beans>  

LoginController.java

package org.Naina.app.controller;

import org.Naina.app.dto.User;
import org.Naina.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/Login")
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping(value="/loginPage", method = RequestMethod.POST)
    public String login(User user, ModelMap map)
    {
        try
        {
            User user2=userService.login(user);
            map.put("msg", user2.getFullName()+" logged in successfully!!");
            return "success";
        }
        catch(NullPointerException e)
        {
            map.put("msg", "The user name and password you entered don't match.");
            return "index";
        }
    }

    @RequestMapping(value="/registrationPage", method = RequestMethod.POST)
    public String add(User user, ModelMap map) {
        int id = userService.add(user);
        if (id != 0) {
            map.put("msg", user.getFullName() + " is registered with the ID : " + id+".");
        }
        return "success";
    }


}

请帮忙。

提前致谢:)

1 个答案:

答案 0 :(得分:0)

您是否将jsp文件放在 WEB-INF 或子文件夹中?如果您这样做,则无法直接访问该文件。