2015-10-09 147 views
0

的web.xmlnoHandlerFound否彈簧MVC

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/application-context.xml</param-value> 
    </context-param> 
    <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>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout>  
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>redirect.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

調度-servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

    <!-- 
    Most controllers will use the ControllerClassNameHandlerMapping above, but 
    for the index controller we are using ParameterizableViewController, so we must 
    define an explicit mapping for it. 
    --> 
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="index.htm">indexController</prop> 
      </props> 
     </property> 
    </bean> 

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

    <!-- 
    The index controller. 
    --> 
    <bean name="indexController" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
      p:viewName="index" /> 

</beans> 

應用的context.xml

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

    <!--bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.url}" 
    p:username="${jdbc.username}" 
    p:password="${jdbc.password}" /--> 

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> 

</beans> 

的index.jsp找到HTTP請求映射

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!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=UTF-8"> 
     <title>Welcome to Spring Web MVC project</title> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> 
    </head> 

    <body> 
     <p>Hello! This is the default welcome page for a Spring Web MVC project.</p> 
     <input type="text" name="uname" id="uname"/> 
     <button onclick="cal('uname')">submit</button> 
     <div id="msg"></div> 
     <script> 
      function cal(uname){ 
       var name = $("#"+uname).val(); 
       var url = "serv/getMsg?uname="+uname; 
       $.ajax({url: url,success: function(data){ 
        $('#msg').html(data); 
       }}); 
      } 
     </script> 
    </body> 
</html> 

serv.java

package com.controller; 
import java.util.*; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
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.ResponseBody; 

@Controller 
@RequestMapping(value="/serv") 
public class serv { 
    @RequestMapping(value = "/getMsg", method = RequestMethod.GET) 
    public @ResponseBody 
    String getMsg(HttpServletRequest req, HttpServletResponse res){ 
     System.out.print("here"); 
     String name = req.getParameter("uname"); 
     //req.setAttribute("name",name); 
     return name; 
    } 
} 

我不KNW地方出了錯..埃夫裏的時間越來越錯誤「無法加載資源:服務器與404狀態響應(未找到)」。幫我解決這個問題....

回答

0

我想你的網絡資源沒有得到掃描,您得到404 你應該在調度員servlet.xml.Please添加像這樣<context:component-scan base-package="com.web.resouces.package" />看到這個鏈接

http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/

希望這會有所幫助。

+0

我這樣做,但它給服務器錯誤 - >服務器遇到一個內部錯誤,阻止它履行這個請求。 –

+0

您是否在服務器啓動或點擊請求/服務器時出錯? 你能否也發佈錯誤? –

+0

服務器啓動.. error->嵌套異常是org.xml.sax.SAXParseException;元素「context:component-scan」的前綴「context」未綁定。 –