Dispatcher servlet(servlet-context.xml)
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/mvc/spring-mvc.xsd 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“>
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.projects.model" />
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://localhost:3306/customerdb" />
<beans:property name="username" value="root" />
<beans:property name="password" value="root" />
</beans:bean>
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan" value="com.projects.model" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
package com.myprojects.controller;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.projects.model.CustomerDao;
import com.projects.model.Customer;
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
private CustomerDao dao;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String customer(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
return "home";
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public String addCustomer(@ModelAttribute("customer") Customer customer, BindingResult result) {
dao.save(customer);
return "home";
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<P> The time on the server is ${serverTime}. </P> --%>
<form action="addCustomer">
Cust_Id :
<input type="text">
<br>
Name :
<input type="text">
<br>
Age :
<input type="text">
<input type="submit" value="Save">
</form>
</body>
</html>
按下save按钮后,我会调用以下url
http://localhost:8080/model/customer
并抛出上述错误。
您的组件扫描标记正在扫描“com.projects.model”包但您的控制器在“com.myprojects.controller”包中更改组件扫描
<context:component-scan base-package="com.projects.model" />
至
<context:component-scan base-package="com.myprojects.controller"/>
那就试试。
我正在尝试学习Spring Security性,第一个代码示例是在运行URL“http://localhost:8080/spring-security-helloworld-xml/welcome”并使用jetty插件作为服务器时出现这样的错误。 错误:org.springframework.web.servlet.pageNotFound noHandlerFound警告:在名为“mvc-di
问题内容: 我正在尝试设置一个Spring MVC项目。我添加了一个调度程序servlet,一个jsp并设置了web.xml文件。但我不断 WARN org.springframework.web.servlet.PageNotFound-在DispatcherServlet中,名称为“ HelloWeb”的URI [/safesite/WEB-INF/jsp/hello.jsp]的HTTP请求未
问题内容: 我正在尝试设置一个Spring MVC项目。我添加了一个调度程序servlet,一个jsp并设置了web.xml文件。但我不断 WARN org.springframework.web.servlet.PageNotFound-在DispatcherServlet中,名称为“ HelloWeb”的URI [/safesite/WEB-INF/jsp/hello.jsp]的HTTP请求未
很抱歉再次问这种问题,但我无法通过查看其他线程和Spring doc来解决我的问题。 我正在使用maven的3.1.0.RELEASE,并尝试使用注释和java配置。 以下是我的web.xml: 这是我的档案web-application-config.xml. 我有两个类。第一个配置视图解析器 第二个定义我的控制器: 根据我的配置,我想一切都应该指向我的home()函数。然而,事实并非如此,以下
我写了一个spring boot项目。它有三个文件。 appconfig.java HelloController.java 当我尝试运行它时,它出现了错误“没有为名为'DispatcherServlet中URI[/springc1_01/]的HTTP请求找到映射”。这是因为服务器没有找到控制器还是其他原因?THX.
这是我的网站。xml 我的servlet上下文。xml 最后是Handler类。这是在com下。比如Spring。控制器。恳求 但是,我们将转到 它返回404错误。 这是我的项目结构