我不明白我在哪里做错了,我无法加载任何页面。以及如何在其他 jsp 页面中提供指向 jsp 页面的链接。就像我在索引页中所做的那样,这会正常工作吗?因为我收到错误(找不到资源(请有人帮忙。这是我的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>
我的控制器.. package 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");
}
}
我的网络.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>
我的春天.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>
我的索引.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>
在你的web中尝试这个映射.xml对于SpringDispathcerServlet servlet:
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
在此答案中,您可以找到更多信息