【问题标题】:resource not available issue in spring mvcspring mvc中的资源不可用问题
【发布时间】:2014-10-25 07:19:39
【问题描述】:

我是 Spring MVC 的新手,并尝试在 Eclipse 中使用 spring 和 Hibernate 创建一个简单的登录模块。 但我收到以下错误

HTTP 状态 404 - /Login.spring/processCredentials

输入状态报告

消息/Login.spring/processCredentials

说明请求的资源不可用。

Apache Tomcat/8.0.12

下面是我的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LoginApp-SpringMVC-Part2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.spring</url-pattern>
  </servlet-mapping>
</web-app>

`

下面是我的控制器类

    package com.pointerunits.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.pointerunits.service.*;


@Controller
@RequestMapping("/Login.spring")
public class LoginController {
    @Autowired //Here the authenticate service is automatically injected into this controller
    private AuthticateService authenticateService;
    @RequestMapping(method =RequestMethod.POST)
    public ModelAndView processCredentials(@RequestParam("userName")String userName,@RequestParam("password")String password)
    {
        System.out.println("processcredentials");
        
        String message = "Invalid credentials";
        if(authenticateService.verifyUserNameAndPassword(userName, password)) {
            message = "Welcome " + userName + "!!";
        }
        return new ModelAndView("results","message",message);
    }
}

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 charset="utf-8">
    <title>Login</title>
    <style type="text/css">
    body {
    background-color: #f4f4f4;
    color: #5a5656;
    font-family: 'Open Sans', Arial, Helvetica, sans-serif;
    font-size: 16px;
    line-height: 1.5em;
    }
    a { text-decoration: none; }
    h1 { font-size: 1em; }
    h1, p {
    margin-bottom: 10px;
    }
    strong {
    font-weight: bold;
    }
    .uppercase { text-transform: uppercase; }
    
    /* ---------- LOGIN ---------- */
    #login {
    margin: 50px auto;
    width: 300px;
    }
    form fieldset input[type="text"], input[type="password"] {
    background-color: #e5e5e5;
    border: none;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    color: #5a5656;
    font-family: 'Open Sans', Arial, Helvetica, sans-serif;
    font-size: 14px;
    height: 50px;
    outline: none;
    padding: 0px 10px;
    width: 280px;
    -webkit-appearance:none;
    }
    form fieldset input[type="submit"] {
    background-color: #008dde;
    border: none;
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    color: #f4f4f4;
    cursor: pointer;
    font-family: 'Open Sans', Arial, Helvetica, sans-serif;
    height: 50px;
    text-transform: uppercase;
    width: 300px;
    -webkit-appearance:none;
    }
    form fieldset a {
    color: #5a5656;
    font-size: 10px;
    }
    form fieldset a:hover { text-decoration: underline; }
    .btn-round {
    background-color: #5a5656;
    border-radius: 50%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    color: #f4f4f4;
    display: block;
    font-size: 12px;
    height: 50px;
    line-height: 50px;
    margin: 30px 125px;
    text-align: center;
    text-transform: uppercase;
    width: 50px;
    }
    .facebook-before {
    background-color: #0064ab;
    border-radius: 3px 0px 0px 3px;
    -moz-border-radius: 3px 0px 0px 3px;
    -webkit-border-radius: 3px 0px 0px 3px;
    color: #f4f4f4;
    display: block;
    float: left;
    height: 50px;
    line-height: 50px;
    text-align: center;
    width: 50px;
    }
    .facebook {
    background-color: #0079ce;
    border: none;
    border-radius: 0px 3px 3px 0px;
    -moz-border-radius: 0px 3px 3px 0px;
    -webkit-border-radius: 0px 3px 3px 0px;
    color: #f4f4f4;
    cursor: pointer;
    height: 50px;
    text-transform: uppercase;
    width: 250px;
    }
    .twitter-before {
    background-color: #189bcb;
    border-radius: 3px 0px 0px 3px;
    -moz-border-radius: 3px 0px 0px 3px;
    -webkit-border-radius: 3px 0px 0px 3px;
    color: #f4f4f4;
    display: block;
    float: left;
    height: 50px;
    line-height: 50px;
    text-align: center;
    width: 50px;
    }
    .twitter {
    background-color: #1bb2e9;
    border: none;
    border-radius: 0px 3px 3px 0px;
    -moz-border-radius: 0px 3px 3px 0px;
    -webkit-border-radius: 0px 3px 3px 0px;
    color: #f4f4f4;
    cursor: pointer;
    height: 50px;
    text-transform: uppercase;
    width: 250px;
    }
    </style>
    </head>
    <body>
    <div id="login">
    <h1><strong>Welcome.</strong> Please login.</h1>
    
      
       <form action="/Login.spring/processCredentials" method="post"><br><br>
       <fieldset>
          User:<br/><input type="text" name="user"/><br/>
          Password:<br/><input type="password" name="password"/>
          <br>
          
          <input type="submit" value="Login"/>  
          </fieldset>   
       </form>
       </div>
        </body>
    </html>

当我调用该操作时,我收到上述错误

我的应用程序上下文文件

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/loginmodule"></property>
        <property name="username" value="root"></property>
        <property name="password" value="akhilsql"></property>  
              
    </bean> 
     <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourceBean"></property>
        <property name="mappingResources">
            <value>com/pointerunits/pojo/User.hbm.xml</value>                   
        </property>
        <property name="hibernateProperties">
              <props>
                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                  <prop key="hibernate.show_sql">true</prop>
              </props>
         </property>
    </bean>
    <bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>
    <bean id="authenticateServiceBean" class="com.pointerunits.service.AuthticateService">
        <property name="hibernateTemplate" ref="hibernateTemplateBean"></property>
    </bean> 
</beans>

mvc 调度程序文件:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.pointerunits.web"></context:component-scan>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

服务文件.java:

package com.pointerunits.service;

import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.pointerunits.pojo.User;
import java.security.MessageDigest;

public class AuthticateService {
    private HibernateTemplate hibernateTemplate;
    
    public AuthticateService() {
        // TODO Auto-generated constructor stub
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
    
    public boolean verifyUserNameAndPassword(String userName,String password) {
        System.out.println("Inside into service class");
        boolean userStatus = false;
        try {
            @SuppressWarnings("unchecked")
            List<User> userObjs = hibernateTemplate.find("from User u where u.user_name=? and u.user_pwd=?",userName,md5(password.toCharArray()));
            if(userObjs.size() != 0) {
                //System.out.println("User ID : " + userObjs.get(0).getUserId() + ", User name : " + userObjs.get(0).getUserName() + ", Password : " + userObjs.get(0).getPassword());
                userStatus = true;
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return userStatus;
    }
    
    private String md5 (char[] c){
        try{
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update((new String(c)).getBytes("UTF8"));
        String enc = new String(md.digest());
       // JOptionPane.showMessageDialog(null,enc);
        return enc;
        }
        catch (Exception ex){
        return "";
        }
    }
}

User.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 8, 2012 9:26:27 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.pointerunits.pojo.User" catalog="loginmodule" table="users">
        <id name="userId" type="long">
            <column name="users_id" />
            <generator class="native" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="user_name" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="user_pwd" />
        </property>
    </class>
</hibernate-mapping>

以下是修改后的代码 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LoginApp-SpringMVC-Part2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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 charset="utf-8">
<title>Login</title>
<style type="text/css">
body {
background-color: #f4f4f4;
color: #5a5656;
font-family: 'Open Sans', Arial, Helvetica, sans-serif;
font-size: 16px;
line-height: 1.5em;
}
a { text-decoration: none; }
h1 { font-size: 1em; }
h1, p {
margin-bottom: 10px;
}
strong {
font-weight: bold;
}
.uppercase { text-transform: uppercase; }

/* ---------- LOGIN ---------- */
#login {
margin: 50px auto;
width: 300px;
}
form fieldset input[type="text"], input[type="password"] {
background-color: #e5e5e5;
border: none;
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
color: #5a5656;
font-family: 'Open Sans', Arial, Helvetica, sans-serif;
font-size: 14px;
height: 50px;
outline: none;
padding: 0px 10px;
width: 280px;
-webkit-appearance:none;
}
form fieldset input[type="submit"] {
background-color: #008dde;
border: none;
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
color: #f4f4f4;
cursor: pointer;
font-family: 'Open Sans', Arial, Helvetica, sans-serif;
height: 50px;
text-transform: uppercase;
width: 300px;
-webkit-appearance:none;
}
form fieldset a {
color: #5a5656;
font-size: 10px;
}
form fieldset a:hover { text-decoration: underline; }
.btn-round {
background-color: #5a5656;
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
color: #f4f4f4;
display: block;
font-size: 12px;
height: 50px;
line-height: 50px;
margin: 30px 125px;
text-align: center;
text-transform: uppercase;
width: 50px;
}
.facebook-before {
background-color: #0064ab;
border-radius: 3px 0px 0px 3px;
-moz-border-radius: 3px 0px 0px 3px;
-webkit-border-radius: 3px 0px 0px 3px;
color: #f4f4f4;
display: block;
float: left;
height: 50px;
line-height: 50px;
text-align: center;
width: 50px;
}
.facebook {
background-color: #0079ce;
border: none;
border-radius: 0px 3px 3px 0px;
-moz-border-radius: 0px 3px 3px 0px;
-webkit-border-radius: 0px 3px 3px 0px;
color: #f4f4f4;
cursor: pointer;
height: 50px;
text-transform: uppercase;
width: 250px;
}
.twitter-before {
background-color: #189bcb;
border-radius: 3px 0px 0px 3px;
-moz-border-radius: 3px 0px 0px 3px;
-webkit-border-radius: 3px 0px 0px 3px;
color: #f4f4f4;
display: block;
float: left;
height: 50px;
line-height: 50px;
text-align: center;
width: 50px;
}
.twitter {
background-color: #1bb2e9;
border: none;
border-radius: 0px 3px 3px 0px;
-moz-border-radius: 0px 3px 3px 0px;
-webkit-border-radius: 0px 3px 3px 0px;
color: #f4f4f4;
cursor: pointer;
height: 50px;
text-transform: uppercase;
width: 250px;
}
</style>
</head>
<body>
<div id="login">
<h1><strong>Welcome.</strong> Please login.</h1>

  
   <form action="/Login.htm" method="post"><br><br>
   <fieldset>
      User:<br/><input type="text" name="user"/><br/>
      Password:<br/><input type="password" name="password"/>
      <br>
      
      <input type="submit" value="Login"/>  
      </fieldset>   
   </form>
   </div>
    </body>
</html>

控制器类:

package com.pointerunits.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.pointerunits.service.*;


@Controller
@RequestMapping("/Login.htm")
public class LoginController {

    @Autowired //Here the authenticate service is automatically injected into this controller
    private AuthticateService authenticateService;
    @RequestMapping(value="/processCredentials" , method =RequestMethod.POST)
    public ModelAndView processCredentials(@RequestParam("userName")String userName,@RequestParam("password")String password)
    {
        System.out.println("processcredentials");
        
        String message = "Invalid credentials";
        if(authenticateService.verifyUserNameAndPassword(userName, password)) {
            message = "Welcome " + userName + "!!";
        }
        return new ModelAndView("results","message",message);
    }
}

【问题讨论】:

  • 如果您是初学者,请从 Spring Boot 开始。它将为您自动配置几乎所有这些,并防止一些常见错误。

标签: java spring hibernate jsp spring-mvc


【解决方案1】:

您的 Spring 操作方法映射到 /Login.spring,方法 POST。您的表单操作是/Login.spring/processCredentials。两者显然不匹配。操作应该是/Login.spring

这也假设您的 webapp 是 Tomcat 中的根 webapp。如果不是,那么您还需要预先添加 webapp 的上下文路径:/myWebApp/Login.spring

还请注意,由于您将 spring servlet 映射到 *.spring,因此对不以 .spring 结尾的 URL 的请求无法发送到 Spring 控制器。

【讨论】:

  • 我将 [code]*.spring 更改为 /[code] 并形成动作以及将弹簧操作方法更改为“/Login.htm”还将控制器类 @RequestMapping 的第 17 行更改为“ [code]@RequestMapping(value="/processCredentials" , method =RequestMethod.POST)"[code] 所以现在错误m 得到的是 HTTP 状态 404 - /Login.htm 类型状态报告消息 /Login.htm 描述 请求的资源不可用。 Apache Tomcat/8.0.12
  • 在您的问题中添加一个显示新代码的部分。此评论令人困惑。
【解决方案2】:

您必须将您在 jsp 中的操作从 Login.spring/processCredentials 更改为 Login.spring 就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2014-01-11
    • 2013-07-27
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多