gpt4 book ai didi

java - SpringMVC 错误

转载 作者:行者123 更新时间:2023-12-01 04:39:37 25 4
gpt4 key购买 nike

案例1:我收到以下错误:

 org.apache.jasper.JasperException: An exception occurred processing JSP page /login.jsp at line 14

11: <body>
12:
13:
14: <form:form method="get" commandName="user" action="login">
15:
16: <form:label path="Username" /><form:input path="uname"/>
17: <form:label path="Password" /><form:input path="password"/>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:424)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:85)
org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:209)
org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:74)
org.springframework.web.servlet.support.JspAwareRequestContext.<init>(JspAwareRequestContext.java:48)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
org.apache.jsp.login_jsp._jspx_meth_form_005fform_005f0(login_jsp.java:111)
org.apache.jsp.login_jsp._jspService(login_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

当 IAM 使用以下表格时:

<form:form  method="post" commandName="user" action="login">

<form:label path="Username" /><form:input path="uname"/>
<form:label path="Password" /><form:input path="password"/>

<input type="submit" value="Submit" />
</form:form>

案例2:当我提交以下表单后,我收到 404,我的意思是这个网址:http://localhost:8443/BugTrackingSystem/login

当 IAM 使用以下表格时:

<form action="login" method="post">
Username: <input type="text" name="uname">
Password: <input type="text" name="password">
<input type="submit" value="Submit">
</form>

其他文件:*登录 Controller :*

package com.bts.controller;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractFormController;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.bts.vo.User;

public class LoginController extends SimpleFormController{


public LoginController(){
setCommandClass(User.class);
setCommandName("user");
}



protected ModelAndView onSubmit(Object obj) throws ServletException {

User user = (User) obj;
System.out.println("username: "+user.getUname());


return new ModelAndView("index","user", user);

}


}

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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>BugTrackingSystem</display-name>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

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



</web-app>

USER.JAVA

package com.bts.vo;

import java.util.ArrayList;
import java.util.List;

public class User {

public User() {

countries = new ArrayList<String>();

countries.add("India");
countries.add("US");

}

private String uname;
private String password;
private List<String> countries;

public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<String> getCountries() {
return countries;
}
public void setCountries(List<String> countries) {
this.countries = countries;
}

}

DISPATCHER-SERVLET.XML

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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">


<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="1">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>


</beans>

CONTROLLERS-BEANS.XML

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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">



<bean name="login" class="com.bts.controller.LoginController">
<property name="successView" value="index" />
</bean>

<bean name="productDetails"
class="com.bts.controller.ProductDetailsController">
</bean>



</beans>

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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">

<import resource="dispatcher-servlet.xml"/>
<import resource="controllers-beans.xml"/>
<import resource="datasource.xml"/>
<import resource="hibernate-beans.xml"/>


</beans>

我现在正在学习Spring,所以我遇到了上述问题,所以请给我一个解决方案,等我理解这个问题后我会使用Annotations..

最佳答案

我推荐以下链接:

http://duckranger.com/2012/04/spring-mvc-dispatcherservlet/

http://viralpatel.net/blogs/spring-3-mvc-handling-forms/

http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

这个设置对我有用:

web.xml

<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
[...]
</web-app>

application-context.xml

<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="myOtherContext.xml"/>
</beans>

myOtherContext.xml(为了进行一些导入)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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">

<bean name="login" class="test.LoginController"/>
</beans>

dispatcher-context.xml (请注意,这是自动寻址的,因为我的调度程序 servlet 称为 dispatcher 并且它附加了 -context.xml 自动)

<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">index</prop>
<prop key="login.htm">login</prop>
</props>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />

<bean name="index"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />

</beans>

login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<form:form method="post" commandName="user" action="login.htm">
<form:label path="uname" /><form:input path="uname"/>
<form:label path="password" /><form:input path="password"/>
<input type="submit" value="Submit" />
</form:form>
</body>
</html>

LoginController.java

public class LoginController extends SimpleFormController {

public LoginController() {
setCommandClass(User.class);
setCommandName("user");
}

@Override
protected org.springframework.web.servlet.ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {

return super.showForm(request, response, errors, controlModel);
}

@Override
protected ModelAndView onSubmit(Object obj) throws ServletException {

User user = (User) obj;
System.out.println("username: " + user.getUname());
return new ModelAndView("index", "user", user);
}
}

关于java - SpringMVC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16768928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com