Sof*_*eer 5 java spring-mvc maven
我尝试放置2请求映射,因为我想指向home / home,因为以后我将拥有另一个具有关于about / home的名称的控制器。但是我不确定为什么它不起作用。如果只有家,它正在工作,但家/家或关于/家不工作。该课程级别无法正常工作。
这个控制器
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.constant.server.HelperConstant;
@Controller
@RequestMapping("/home")
public class HomeController
{
@RequestMapping("/home")
public String doDisplayPage()
{
return HelperConstant.VIEW_HOME;
}
}
Run Code Online (Sandbox Code Playgroud)
这个jsp
<html>
<body>
<form action="home/home">
<input type="text" name="t1"><br>
<input type="text" name = "t2"><br>
<input type ="submit">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我正在点击HTTP状态404-
已编辑
附加配置文件
package com.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[] {SpringConfig.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] {"/"};
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一个配置文件
package com.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan({"com.controller"})
public class SpringConfig
{
@Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver vr = new InternalResourceViewResolver();
//vr.setPrefix("/WEB-INF/");
vr.setSuffix(".jsp");
return vr;
}
}
Run Code Online (Sandbox Code Playgroud)
还有另外一个发现,据此,我试图在home / home.jsp中寻找jsp。我能知道为什么这样的行为吗?正确的应该只看home.jsp。如果是remvoe类级别的注释,那么它可以正常工作,或者我在webapp home内创建了一个文件夹,它可以正常工作,但是再次单击提交后,它将查找home / home / home.jsp
我添加了一个方法=“ POST”
现在问题是重新启动后
点击后提交像这样
如果再次单击提交,则显示错误404
这是我的web.xml,但是我已经删除了它,因为我已经使用过Java或配置
<!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>
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>home.html</welcome-file>
<welcome-file>home.htm</welcome-file>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)
小智 0
您的 Spring 应用程序类是什么样的?如果您没有,您应该创建一个。不要使用@ComponentScan下面的@Configuration标签,而是执行如下所示的操作,应该更容易找到它,也可以将根标签更改为/更易于阅读:
@SpringBootApplication
@ComponentScan(basePackages = {"com.*"})
class MyClass {
public static void main(String[] args) {
SpringApplication.run(MyClass.class)
}
}
@Controller
@RequestMapping("/")
public class HomeController
{
@RequestMapping("/home")
public String doDisplayPage()
{
return HelperConstant.VIEW_HOME;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
316 次 |
| 最近记录: |