请问下为什么我的请求映射不到我的controller里面?

AndrewHuang0531 2015-04-11 12:22:57
这是我的controller层:

@Controller
@RequestMapping("/user")
public class UserController {


@RequestMapping("/register")
public ModelAndView register(String username){
System.out.println("1");
ModelAndView mav = new ModelAndView();
mav.setViewName("hi");
return mav;
}
}

这是我的jsp提交页面:
<form method="post" action="user/register">

<input type="text" name="username">
<input type="submit" value="login" >
</form>

这是我的web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bao-servlet.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bao.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>


这是我的spring配置文件

<?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"
xmlns:util="http://www.springframework.org/schema/util"
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-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.yue.controller"/>


<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix = "/WEB-INF/views"
p:suffix = ".jsp"
/>


</beans>

折腾了一下午了,求大神帮我说说问题。。。
...全文
572 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
microhex 2015-04-12
  • 打赏
  • 举报
回复
<servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> 你的url-pattern必须存在后缀,如*.do , *.action 如果没有这样的话 ,spring的DispatcherServlet会把你的url将你的url重复请求(也就是你返回的结果也会去匹配DispatcherServlet),这样会一直得不到结果的。。。。 如果用 <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 的话,相应的访问地址应该为:user/register.do 。。。。。 上面的是错的啊。。。都不知道按个快捷键就走了,擦
microhex 2015-04-12
  • 打赏
  • 举报
回复
<servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> 你的url-pattern必须存在后缀,如/*.do ,/ *.action 如果没有这样的话 ,spring的DispatcherServlet会把你的url将你的url重复请求(也就是你返回的结果也会去匹配DispatcherServlet),这样会一直得不到结果的。。。。 如果用 <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/*.do</url-pattern> </servlet-mapping> 的话,相应的访问地址应该为:user/register.do 。。。。。
oh_Maxy 2015-04-12
  • 打赏
  • 举报
回复
<servlet-name>appServlet</servlet-name> <url-pattern>/*</url-pattern> 这个servlet会拦截所有的请求,包括jsp页面,效果可能是,你访问某个jsp页面,结果返回404或者一片空白。 可以考虑吧所有的action定义成类似*.htm,然后你的servlet的url-pattern配置为*.htm
波音天上飞 2015-04-12
  • 打赏
  • 举报
回复
<form method="post" action="user/register">,这个改成<form method="post" action="/user/register">试一下,如果还不行,应该是搭建环境时,配置xml出问题了
oh_Maxy 2015-04-12
  • 打赏
  • 举报
回复
确定是没进入Controller,还是跳转到hi.jsp时报错了?
IBelieve_ICanFly 2015-04-12
  • 打赏
  • 举报
回复
哥们,细心点: @RequestMapping(value="/register", method=RequestMethod.POST)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧