<?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:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.cgi.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/JSPS/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/tutorial" />
<property name="username" value="root" />
<property name="password" value="tiger" />
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="mappingResources">
<list><value>Employee.hbm.xml</value></list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"/>
</bean>
<bean id="LoginDao" class="com.cgi.dao.LoginDaoImpl">
<property name="ht" ref="hibernateTemplate"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Login Example</display-name>
<welcome-file-list>
<welcome-file>Login-Form.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>emp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Controller-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>emp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/emp-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.cgi.beans.Employee;
import com.cgi.dao.LoginDaoImpl;
import java.util.*;
@Controller
public class EmployeeController
{
LoginDaoImpl d;
@RequestMapping(value="/Check", method=RequestMethod.POST)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String name=request.getParameter("username");
String password=request.getParameter("pass");
List<Employee> b=(List<Employee>) d.checkuser(name, password);
System.out.println("Hello");
Map m=new HashMap();
m.put("msg", "Hello"+name);
if(b.size()>0)
{
return new ModelAndView("Success",m);
}
else
{
return new ModelAndView("Fail");
}
}
@RequestMapping(value="/GetAll", method = RequestMethod.POST)
public ModelAndView getAll(HttpServletRequest request, HttpServletResponse response) throws Exception
{
List<Employee> l=d.getall();
Map m=new HashMap();
m.put("msg", l);
return new ModelAndView("AllUsers",m);
}
@RequestMapping("/Register.htm")
public ModelAndView register(HttpServletRequest req, HttpServletResponse resp) throws Exception
{
ApplicationContext ax=new ClassPathXmlApplicationContext("emp-servlet.xml");
Employee e=(Employee)ax.getBean("d");
d.save(e);
return new ModelAndView("AllUsers");
}
}
这是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">
<title>Login Form</title>
</head>
<body>
<form method="post" action="/emp/Check">
<center>
<pre>
User-Name: <input id="name" type="text" name="username" required ><br>
Password: <input id="pswd" type="password" name="pass" required><br>
<input type="submit" value="Submit"><br>
</pre>
</center>
</form>
</body>
</html>
问题是,虽然在web.xml
中有正确的映射方案,但从jsp来看,它不能用于我的控制器。我已经检查了我的网站,但他们都显示相同。如果有人能在他们的系统上运行它并检查它,请…请尽快恢复。
错误报告。
在路径为[/employee_management_tool]的上下文中,servlet[emp]的servlet.service()引发异常[请求处理失败;嵌套异常是java.lang.NullPointerException],根原因是java.lang.NullPointerException位于com.cgi.controller.employeeController.handlerequest(EmployeeController.java:28)位于sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)位于d.annotation.support.handlerMethodInvoker.doinvokeMethod(handlerMethodInvoker.java:710)在org.springframework.web.bind.annotation.support.handlerMethodInvoker.invoke.invokeMethod(handlerMethodInvoker.java:167)在org.springframework.web.servlet.mvc.annotation.annotation.annotation.annotationMethod(annotlerMethodInvoker.java:167)在tationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)在org.springframework.web.servlet.dispatcherServlet.doDispatch(DispatcherServlet.java:771)在org.springframework.web.servlet.dispatcherServlet.doService(DispatcherServlet.java:716)在org.springframework.web.servlet.frameworkServlet.processRequest(服务(httpservlet.java:729),org.apache.catalina.core.applicationFilterChain.InternaldoFilter(applicationFilterChain.java:291),org.apache.catalina.core.applicationFilterChain.doFilter(applicationFilterChain.java:206),org.apache.catalina.core.wsfilter.doFilter(applicationFilterChain.java:206),org.apache.tomcat.websocket.wsfilter.doFiltercationFilterChain.java:206)在org.apache.catalina.core.standardWrapperValve.invoke(StandardWrapperValve.java:212)在org.apache.catalina.core.standardContextValve.invoke(StandardContextValve.invoke(StandardContextValve.java:106)在valves.abstractAccessLogValve.invoke(abstractAccessLogValve.java:616)在org.apache.catalina.core.standardEnginEvalve.invoke(StandardEnginEvalve.java:88)在org.apache.catalina.connector.coyoteAdapter.Service(coyoteAdapter.java:521)在org.apache.coyote.http11.abstractThttp11Processor.Process(abstractThttp11Processore.java:1096)在nioEndpoint.java:1500)在org.apache.tomcat.util.net.nioEndpoint$socketprocessor.run(nioEndpoint.java:1456)在java.util.concurrent.threadpoolexecutor.runworker(未知源)在java.util.concurrent.threadpoolexecutor$worker.run(未知源)在org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java.lang.thread.run(未知源)
>
检查web.xml,“context configlocation”被提到了两次。见下文
<servlet>
<servlet-name>emp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Controller-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
再用另一个值
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/emp-servlet.xml</param-value>
</context-param>
我认为您应该删除第二个,并用值'/WEB-INF/emp-servlet.xml'更正第一个。
如何通过Ajax调用将jsonArray发布到spring控制器?? 这是我的JSON数组想通过这个!!
我有两个实体A和B,其中A拥有B类的属性“B ”( A和B之间一对一的关联) 我想做一个Rest调用来保存A实体的实例,并将b param作为url传递,该url在json中给出类似的东西: /b/2引用B储存库的findById rest资源。 当我执行A Repository的这个目标rest resource save()(通常是对url/restapi/A的POST请求)时,它工作得很好,
我正在使用Spring形式。我只需要得到Staemap作为响应,但我得到的是整个jsp页面作为响应。
问题内容: 我必须将数据从html页面(带有很少输入文本字段的简单形式)发送到页面控制器,然后再发送到数据库。我正在使用3.0版的百里香2.0.17。我搜索并检查了一些解决方案,但是没有用。也许有人遇到了同样的问题,并找到了一些好的解决方案。请帮忙。谢谢 问题答案: 如本教程所建议,你需要使用,并在中创建一个表单。 看起来像这样: 控制器: HTML: Foo.java: 希望这可以帮助。
我是百里香菜鸟。我试图创建一个简单的crud应用程序。我试图在删除按钮上删除客户类的对象。我如何使用百里香叶为调用deleteUser的方法设置参数(例如- id)。这是我的控制器。 以下是我的看法。
我试图将JSON发送到Spring MVC控制器。在Spring MVC方面,所有的配置都是正确的。 下面是代码,但似乎没有运行: 你知道我哪里出错了吗?我对JSON是新手。我试图将JSON发送到Spring MVC控制器。 当调用/application/run/save时,我会得到一个JSON响应。但是,@RequestBody不能工作。 我还是没有运气。读过一些很多类似的问题。需求是服务器将