我正在尝试创建一个简单的应用程序,其中涉及一个jsp页面(这是一个用于输入查询的文本区域和一个提交按钮),一个Query类(如下所示的非常简单的类)和一个QueryController来与两者进行交互。我正在尝试将QueryController打印到控制台进行测试,但是没有输出被打印到Standard.out
。单击提交按钮将我带到http://localhost:8080/<PROJECT_NAME>/?queryField=<QUERY_TEXT>
,这是404错误,因为它不是有效的网页。三个[简单]类如下所示。感谢帮助。
查询类:
public class Query {
private String query;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}
query.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>
<body>
<form action="query" method="post" commandName="queryForm">
<textarea name="queryField" cols="55" rows="1"></textarea><br>
<input type="submit" value="submit">
</form>
</body>
</html>
和我简单的QueryController.java:
@Controller
@RequestMapping(value = "/query")
public class QueryController {
@RequestMapping(method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("queryForm") Query query,
Map<String, Object> model) {
// for testing purpose:
System.out.println("query (from controller): " + query.getQuery());
return "someNextPageHere";
}
}
最佳答案
我们需要对Spring
模块进行更多配置才能使其正常工作。您可以使用Option 1 - with web.xml
或Option 2 - without web.xml
:
选项1(web.xml)
1.将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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>SimpleProject</display-name>
<servlet>
<servlet-name>SimpleProjectServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/SimpleProjectServlet-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SimpleProjectServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>query.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.使用路径-
WEB-INF/config/SimpleProjectServlet-servlet.xml
创建文件3.将以下内容添加到步骤2中创建的文件中。您需要编辑Spring版本引用:
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.mycompany.myproject" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
4.在上述配置中,在
context:component-scan
处设置正确的软件包名称。选项2(非web.xml)
1.删除您的
web.xml
2.为基于注释的
Java Config
添加Spring MVC
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@EnableWebMvc
@Configuration
@ComponentScan({ "com.mycompany.myproject" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
3.在
SpringWebConfig
处使用正确的包修改@ComponentScan
4.为
Java Config
添加WebApplication
。创建具有所需配置的扩展WebApplicationInitializer
的类:import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyWebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringWebConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
更新资料
在这里,我们只有很少的配置部分会导致此错误:
DispatcherServlet
被配置为接受views/*
的url部署描述符没有
src\main\java
到WEB-INF\classes
用户在
Spring 3
上运行的Tomcat 8
默认为JDK-8
,并且ASM Loader
无法加载文件。必须移至Spring版本4.0.1-RELEASE