我是Spring框架的初学者。我试图在Spring MVC框架中构建一个购物项目。该项目是实施购物车(但规模较小)。它包含2个JSP文件作为视图,用于接收用户请求并对其进行响应,即ViewAddproduct.jsp
和ViewCart.asp
。此外,该项目还有4个文件pom.xml
,web.xml
,NikiAbb-servlet.xml
和AddController.java
。 (我打开Maven项目并添加所有必需的依赖项)似乎我的文件中的所有代码或添加的依赖项都是正确的,我可以在服务器(Tomcat)上单独运行每个页面。但是当我运行整个包时,我得到错误404.我已经检查了很多教程来解决它,但它没有工作。任何帮助,将不胜感激。
我正在使用JDK 9.0.4,面向Web开发人员的Eclipse Java EE IDE。氧气2释放(4.7.2)和Tomcat 9
包层次结构:
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>NikiAbb</groupId>
<artifactId>Testut</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Testut Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<properties>
<java.version>9.0.4</java.version>
</properties>
<build>
<finalName>Testut</finalName>
</build>
</project>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app >
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>NikiAbb</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NikiAbb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
NikiAbb-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package = "NikiCart" />
<bean class
= "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
</beans>
Addcontroller.java
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
public class AddController
{
@RequestMapping("/add")
public ModelAndView add(HttpServletRequest request,HttpServletRespose
response)
{
int H-box=Integer.parseInt(request.getParameter("horse-box");
int H-unit=Integer.parseInt(request.getParameter("horse-unit");
int P-box=Integer.parseInt(request.getParameter("ping-box");
int P-unit=Integer.parseInt(request.getParameter("ping-unit");
AddService as = new AddService();
if (H-box=!"") || (H-uint=!)
{
int k = as.add(H-box,H-uint,0);
}else if(P-box=!"") || (P-uint=!)
{
int k = as.add(P-box,P-uint,1);
}
ModelAddview mv = new ModelAndView();
mv.setViewName("ViewCart.jsp");
mv.addObject("Apro-price",k);
return mv;
}
}
ViewAddproduct.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">
</head>
<body>
<h1 align="center">Wellcome To Online Shopping</h1>
<h3 align="center"><font color="blue">Get 10% Discount for shopping 3 box
or more</font></h3>
</br></br>
<form action ="add">
<table border="1" align="center">
<tr>
<td align="center"><img
src="http://www.yourpresents.co.uk/user/products/large/penguine-figurine- trinket-box.jpg" alt="penguin" height="420" width="400"/>
</br></br>
<font size=5>Penguin-jewelry-box</font></br>
<font size=3>Price per box : 175 Nok</br>
20 Unit in each box</font>
</br></br><font size=3>Number Of Box : </font><input name="ping- box" type="number" min="0" max="50" step="1" value ="0"/>
</br></br><font size=3>Number Of Units : </font><input name="ping- unit" type="number" min="0" max="50" step="1" value ="0"/>
</br>
</br>
<input type = "button" value = "Add To Cart"/>
</br>
</td>
<td align="center" height="420" width="400"><img
src="https://cdn0.rubylane.com/shops/632271/005849.1L.jpg" alt="horse"
height="320" width="300">
</br></br></br></br></br></br></br>
<font size=5>HorseShoe-Card</font>
</br>
<font size=3>Price per box : 829 Nok</font></br>
5 Units in each box</font>
</br></br><form:label path = "horse-box"><font size=3>Number Of
Box : </font></form:label><form:input path="horse-box"
</br></br><form:label path = "horse-unit"><font size=3>
Number Of Units : </font></form:label><form:input
path="horse- unit" type="number" min="0" max="50" step="1" value ="0"/>
</br></br>
<input type = "button" value = "Add To Cart"/>
</br>
</td>
</tr>
</table>
</form>
</body>
</html>
ViewCart.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!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">
</head>
<hr width="1700" size="2" color="black">
<table border="0.5" align="left">
<tr align="center"><td width="400">
<img
src="http://www.yourpresents.co.uk/user/products/large/penguine-figurine- trinket-box.jpg" alt="penguin" height="220" width="200"/> </td>
<td width="300">
<font size=5 color="blue">${product-name}</font></td>
<td width="700" align="right"><font size=3>Amount:</font></br>
<font size=3>${Aprod-box}</font><font size=3>Box</font>
</br>
<font size=3>${Aprod-unit}Units</font>
</br></br></br></br></br><font size=3></font>Price:
<font size=4>${Apro-price}</font>
</td>
</tr>
<tr><td width="700">
<hr width="1700" size="1" color="black"></td>></tr>
<tr align="cente" ><td width="700" align="left">
<font size=4>Total:</font>
<td width="400" align="right"><font size=5>${price-total} Nok</font>
</td>
</tr>
<tr> <td>
</br></br></br>
<font align="center"></font></font><input type = "submit"
style="height:50px;width:250px" value = "continoue to shopping"/>
<input type = "submit" align="center" style="height:50px;width:250px"
value = "continue shopping"/>
</td></tr></table>
</body>
</html>
答案 0 :(得分:0)
@ NikiAbb-servlet.xml文件<property name = "prefix" value = "/WEB-INF/jsp/" />
应该<property name = "prefix" value = "/WEB-INF/" />
无法在WEB-INF下看到任何jsp文件夹。
@ Addcontroller.java
@RequestMapping("/add")
public ModelAndView.add(HttpServletRequest request,HttpServletRespose rsponse)
应删除&#34;。&#34; @RequestMapping("/addProd")
public ModelAndView add(HttpServletRequest
。甚至你的RequestMapping也错了。
另一件事mv.setViewName("ViewCart.jsp");
,无需再次添加.jsp,只需更改为mv.setViewName("ViewCart");
。