[英]How can i use my Controller in spring mvc?
I have a problem with my Controller class: I create my project with maven and I add the dependencies for hibernate and Spring. 我的Controller类有问题:我使用maven创建项目,并添加了hibernate和Spring的依赖项。 When I start the app I can go in the first page from the servlet, but when I call any method I get 404. I try many solutions, but no one work, I use a web logic server, there is some particular configuration for this server? 启动应用程序时,我可以从servlet进入第一页,但是当我调用任何方法时,都会得到404。我尝试了许多解决方案,但是没有人使用,我使用Web逻辑服务器,对此有一些特殊的配置。服务器? In the project this is my actual situation: 在项目中,这是我的实际情况:
AppController.java AppController.java
@Controller
@RequestMapping("/")
public class AppController {
@RequestMapping(value = "/two", method = RequestMethod.GET)
public String two() {
System.out.println("work?");
return "newFile";
}
}
i try also with ModelAndView 我也尝试使用ModelAndView
registration.jsp registration.jsp
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title</title>
<style type="text/css">
form {
display: inline-block;
}
</style>
</head>
<body>
<form action="/two" method="GET">
<p class="message">//Not registered?
<input type="submit" value="Create an account">
</p>
</form>
</body>
</html>
web.xml 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>
<servlet>
<servlet-name>LDSServlet</servlet-name>
<servlet-class>com.reply.servlet.LDSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LDSServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
applicationContext.xml applicationContext.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: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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
AppConfig.java AppConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.reply.utility")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
mesageSource.setBasename("messages");
return messageSource;
}
}
pom.xml 的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.reply</groupId>
<artifactId>LDSSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>LDSSpring</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<properties>
<springframework.version>5.1.0.RELEASE</springframework.version>
<hibernate.version>5.3.1.Final</hibernate.version>
<oracle.connector.version>18.1.0.1</oracle.connector.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss/jandex -->
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>2.0.5.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
In the JSP and in the method I try to write the value for call method with / and without, but the result is the same. 在JSP和方法中,我尝试使用/和不使用/编写调用方法的值,但是结果是相同的。 Can someone help me? 有人能帮我吗?
If I understand the problem correctly, you having new controller component and request mapping which should be invoked on form submission. 如果我正确理解问题,则您有新的控制器组件和请求映射,应在表单提交时调用它。
I see the below issues in xml configuration 我在xml配置中看到以下问题
you are not using spring DispatcherServlet
in web.xml . 您没有在web.xml中使用spring DispatcherServlet
。 This class is responsible for processing and handles the request and forwards to appropriate controller 此类负责处理和处理请求,并转发给适当的控制器
You are defining the mapping in web.xml with class com.reply.servlet.LDSServlet
. 您正在使用com.reply.servlet.LDSServlet
类在web.xml中定义映射。 how would the LDSServlet
class look like? LDSServlet
类的外观如何?
Basically DispatcherServlet
, will process the configuration (Controller classes and request mappings) and uses this for picking the appropriate controller while request arrives. 基本上DispatcherServlet
,将处理配置(控制器类和请求映射),并在请求到达时使用它来选择适当的控制器。
EDIT: 编辑:
you should be including the context-path
in the form action. 您应该在表单操作中包含context-path
。
try this way, it is work fine with me. 尝试这种方式,对我来说很好。
@Controller
public class HelloController {
@RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is welcome page!");
model.setViewName("hello");
return model;
}
I resolve my problem, the process "oracle xe tns listener" use my port 8080, i suppose the server block the call on controller. 我解决了我的问题,进程“ oracle xe tns listener”使用我的端口8080,我想服务器阻塞了控制器上的调用。 I found this solution because i try to start my application on tomcat and i had the port busy, so I termitate the process and my application go on tomcat and web logic server :) 我找到了这个解决方案,因为我尝试在tomcat上启动我的应用程序,并且端口繁忙,因此我终止了该过程,并且我的应用程序在tomcat和Web逻辑服务器上进行了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.