2017-02-06 34 views
-1

我无法理解我在哪里做错误我无法加载任何页面.. 另外如何在其他jsp页面提供jsp页面的链接。就像我在索引页面中那样,这样工作会好吗?因为我得到错误(资源未找到) 请别人帮忙。 这是我的servlet文件我试图寻找解决方案“没有映射发现HTTP URI请求[/ DemoApp /索引]在DispatcherServlet与名称'servlet'”

<?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" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 
<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <!-- <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> --> 
    <property name="prefix"> 
     <value>WebContent/WEB-INF/jsp/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 
<mvc:annotation-driven/> 
<context:component-scan base-package="com.demoapp.controller"></context:component-scan> 
</beans> 

我控制器.. 包com.demoapp.controller;

import java.io.IOException; 
    import java.util.List; 


    import org.codehaus.jackson.JsonParseException; 
    import org.codehaus.jackson.map.JsonMappingException; 
    import org.codehaus.jackson.map.ObjectMapper; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestBody; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.bind.annotation.ResponseBody; 

    import com.demoapp.entities.Student; 
    import com.google.gson.Gson; 
    import com.google.gson.GsonBuilder; 
    import com.demoapp.service.StudentService; 

    @Controller 
    public class StudentController { 

    StudentController() { 

    System.out.println("Hello. Reached Controller"); 
} 

@Autowired 
StudentService studentService; 

@RequestMapping(value = "/", method= RequestMethod.GET) 
public String indexPage() { 
    System.out.println("Hello. Index page Loading..."); 
    return "index"; 
} 

@RequestMapping(value = "/addStudent", method = RequestMethod.POST) 
public @ResponseBody void addStudentToDB(@RequestBody String student) { 
    ObjectMapper mapper = new ObjectMapper(); 
    System.out.println("student"); 
    Student studentObj = null; 
    try { 
     studentObj = mapper.readValue(student, Student.class); 
    } catch (JsonParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JsonMappingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    studentService.addStudent(studentObj); 
    System.out.println("Student Added"); 

} 

@RequestMapping(value = "/viewStudents", method = RequestMethod.GET) 
public @ResponseBody String viewAllStudents() { 
    List<Student> students = studentService.getStudents(); 
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create(); 
    String jsonStudents = gson.toJson(students); 
    System.out.println("Sending All Students"); 
    return jsonStudents; 
} 

@RequestMapping(value = "/deleteStudent", method = RequestMethod.POST) 
public @ResponseBody void deleteStudentFromDB(@RequestBody String studentId) { 
    System.out.println(studentId); 
    int id = Integer.parseInt(studentId); 
    studentService.deleteStudent(id); 
    System.out.println("Student Deleted"); 

} 
} 

我的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" 
id="WebApp_ID" version="3.0"> 
<display-name>DemoApp</display-name> 

<servlet> 
    <servlet-name>servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 



<servlet-mapping> 
    <servlet-name>servlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
<!-- <welcome-file-list> 
    <welcome-file>/WEB-INF/pages/index.jsp</welcome-file> 
</welcome-file-list> --> 

</web-app> 

我spring.xml

<?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" 
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 
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-3.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

<context:annotation-config></context:annotation-config> 
<context:component-scan base-package="com.demoapp"></context:component-scan> 

<bean id="transactionMangager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property> 
</bean> 


<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- To Create An Object Of CommonServiceImpl --> 
<bean id="studentService" class="com.demoapp.serviceimpl.StudentServiceImpl"></bean> 

<!--Down Autowire All The DAOs to the Respective DAOImpl classes --> 
<bean id="studentDAO" class="com.demoapp.daoimpl.StudentDAOImpl"> 
</bean> 


</beans> 

我的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="c" 
     uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
     <a href="<c:url value="AddStudent"/>">Add Student</a><br> 
     <a href="<c:url value="ViewStudents"/>">View Students</a><br> 
     <a href="<c:url value="DeleteStudent"/>">Delete Student</a><br> 
</body> 
</html> 

回答

-1

在web.xml的试试这个映射SpringDispathcerServlet servlet:

<servlet-mapping> 
    <servlet-name>servlet</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

在这个answer,你可以找到更多信息

+0

仍然收到此错误 –

+0

尝试URL'/ DemoApp “没有在DispatcherServlet的发现HTTP请求与URI [/ DemoApp /索引]名为 '的servlet' 映射” /'而不是'/ DemoApp/index' – jlumietu

+0

当我在服务器上运行项目时出现此错误 2017年2月6日下午2:42:46 org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:在名为'servlet'的DispatcherServlet中找不到使用URI [/ DemoApp /]的HTTP请求的映射 –

相关问题