求助:SSM整合form到不了controller

weixin_38521872 2017-09-01 03:03:53
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" id="WebApp_ID" version="3.0">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>login.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!--Spring核心监听器 -->
<!--在服务器启动时加载Spring容器,且只会加载一次 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>

<!--配置Springmvc核心控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!--配置由Spring 提供的针对中文乱码的编码过滤器 -->
<!-- 编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>

spring.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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--引入propertise文件 -->
<!--传统方式引入 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!-- <property name="locations" value="classpath:db.properties"></property> -->
<!-- </bean> -->

<!--简化方式 -->
<context:property-placeholder location="classpath:db.properties"/>

<!--1.配置数据源:c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${user}" />
<property name="password" value="${passwd}" />
</bean>

<!--2.配置mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.ssm.bean" />
</bean>

<!-- 3. 自动扫描mybatis映射文件和接口的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"></property>
</bean>

<!--4.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--5.开启注解进行事务管理 transaction-manager:引用上面定义的事务管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

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

<!--1.开启Springioc 自动扫描注解包 -->
<context:component-scan base-package="com.ssm.bean"/>

<!--2. 开启注解 -->
<mvc:annotation-driven />

<!--3.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!--4.注解映射器(可省) -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->

<!--5.配置适配器(不需时可省) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<!-- 在业务方法的返回值和权限之间使用@ResponseBody注解表示返回值对象需要转成JSON文本 -->
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
</beans>


controller

package com.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.bean.User;
import com.ssm.service.UserService;

/**
* 控制层
*
*
*
*/

@Controller
@RequestMapping("/user")
public class UserAction {
// 注入Service
@Autowired
private UserService userService;

@RequestMapping("regist")
public String regist(User user, Model model) {

System.out.println("用户注册:" + user.getUsername() + user.getPassword());

// user.setId(1);
userService.regist(user);

model.addAttribute("msg", user.getPassword());
// 注册成功后跳转success.jsp页面
return "success";
}

@RequestMapping("login")
public String login(String name, String password, Model model) {

System.out.println("用户登录:" + name + password);

/*
* Map<String, String> map=new LinkedHashMap<String,String>();
*
* map.put("name", user.getName()); map.put("password",
* user.getPassword());
*/

userService.login(name, password);

model.addAttribute("msg", "登录成功");

return "success";
}
}
前台页面
[code=html]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/login.action">
<table border="1">
<tr>
<td>用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>

[/code]
...全文
589 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
未詺x 2017-09-06
  • 打赏
  • 举报
回复
heliu3 2017-09-05
  • 打赏
  • 举报
回复
html上加上method=post,controller注解加上@RequestMapping(value = "/upload", method = RequestMethod.POST)试试
未詺x 2017-09-05
  • 打赏
  • 举报
回复
所以会路径不对 爆404
未詺x 2017-09-05
  • 打赏
  • 举报
回复
我已经看出来了 你的控制器 @RequestMapping("/kddy") 少了/
podd 2017-09-04
  • 打赏
  • 举报
回复
断点调试
weixin_38521872 2017-09-03
  • 打赏
  • 举报
回复
引用 12 楼 lrx2011 的回复:
打开控制台去看这个路径是否是只想success.jsp的路径。 直接在浏览器输入这个地址看能不能打开success页面 如果路径什么的都没问题,再去看页面映射的那块配置
现在的问题是我在controller里一进去就要在控制台输出一句话,但是现在控制台根本没反应,说明都没走到那一步啊
新IT民工 2017-09-01
  • 打赏
  • 举报
回复
f12就可以了。
All88In 2017-09-01
  • 打赏
  • 举报
回复
前台的控制台看看请求路径是什么
podd 2017-09-01
  • 打赏
  • 举报
回复

打开控制台去看这个路径是否是只想success.jsp的路径。
直接在浏览器输入这个地址看能不能打开success页面
如果路径什么的都没问题,再去看页面映射的那块配置
新IT民工 2017-09-01
  • 打赏
  • 举报
回复
两种办法1.web.xml里面.action换成/*或者controller里面的地址写成xxx.action
weixin_38521872 2017-09-01
  • 打赏
  • 举报
回复
引用 7 楼 qq_38931197 的回复:
[quote=引用 6 楼 weixin_38521872 的回复:] [quote=引用 5 楼 qq_38931197 的回复:] [quote=引用 4 楼 weixin_38521872 的回复:] [quote=引用 2 楼 qq_38931197 的回复:] .action去掉
是哪里的action? 能进入登录和注册页面 是填写完后跳转404 [/quote] 界面 <form action="${pageContext.request.contextPath }/user/login.action">[/quote] 这个也试过了 ,还是一样 ,需不需要我提供其他信息[/quote] @RequestMapping("login")加上.action[/quote]刚试过,也一样...
weixin_38521872 2017-09-01
  • 打赏
  • 举报
回复
引用 8 楼 ilovelizehui 的回复:
你web.xml里面配置的是*.action的话。。在controller的requestmappling 里面用 xxx.action
刚试过,也一样...
「已注销」 2017-09-01
  • 打赏
  • 举报
回复
你web.xml里面配置的是*.action的话。。在controller的requestmappling 里面用 xxx.action
qq_天使在唱歌 2017-09-01
  • 打赏
  • 举报
回复
引用 6 楼 weixin_38521872 的回复:
[quote=引用 5 楼 qq_38931197 的回复:] [quote=引用 4 楼 weixin_38521872 的回复:] [quote=引用 2 楼 qq_38931197 的回复:] .action去掉
是哪里的action? 能进入登录和注册页面 是填写完后跳转404 [/quote] 界面 <form action="${pageContext.request.contextPath }/user/login.action">[/quote] 这个也试过了 ,还是一样 ,需不需要我提供其他信息[/quote] @RequestMapping("login")加上.action
weixin_38521872 2017-09-01
  • 打赏
  • 举报
回复
引用 5 楼 qq_38931197 的回复:
[quote=引用 4 楼 weixin_38521872 的回复:] [quote=引用 2 楼 qq_38931197 的回复:] .action去掉
是哪里的action? 能进入登录和注册页面 是填写完后跳转404 [/quote] 界面 <form action="${pageContext.request.contextPath }/user/login.action">[/quote] 这个也试过了 ,还是一样 ,需不需要我提供其他信息
qq_天使在唱歌 2017-09-01
  • 打赏
  • 举报
回复
引用 4 楼 weixin_38521872 的回复:
[quote=引用 2 楼 qq_38931197 的回复:] .action去掉
是哪里的action? 能进入登录和注册页面 是填写完后跳转404 [/quote] 界面 <form action="${pageContext.request.contextPath }/user/login.action">
weixin_38521872 2017-09-01
  • 打赏
  • 举报
回复
引用 2 楼 qq_38931197 的回复:
.action去掉
是哪里的action? 能进入登录和注册页面 是填写完后跳转404
weixin_38521872 2017-09-01
  • 打赏
  • 举报
回复
@qq_38931197 是哪里的action? 能进入登录和注册页面 是填写完后跳转404
qq_天使在唱歌 2017-09-01
  • 打赏
  • 举报
回复
.action去掉
weixin_38521872 2017-09-01
  • 打赏
  • 举报
回复
控制台无报错 页面提示是404-Not Found

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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