求助帖:ssm项目框架自己搭建,遇到的前后端交互问题
问题:
jsp无法传值到控制层;
以下是项目结构和配置文件:
结构:
配置文件:
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:property-placeholder location="WEB-INF/config/jdbc.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="maxWait" value="${jdbc.maxWait}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxActive" value="${jdbc.maxActive}" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:Project/Mapper/*.xml" />
<!-- 指定Mapper总的配置文件路径,定制的environment在spring容器中不再生效 -->
<property name="configLocation" value="WEB-INF/config/mapperConfig.xml" />
</bean>
<!-- 配置MyBatis注解 -->
<!-- Spring会根据这段配置,自动扫描带有指定注解的接口, 然后自动创建这个接口的实现类,并使用对应的xml 中的SQL来实现对应的方法。
即,Spring会自动扫描指定包下,带有注定注解的接口。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包 -->
<property name="basePackage" value="Project.Dao" />
<!-- 指定要扫描的注解,需要自定义 -->
<property name="annotationClass" value="Project.annotation.MybatisDao" />
</bean>
<!-- 注解版本的 MVC配置 -->
<context:component-scan base-package="Project.*" />
<!-- 开启MVC注解 -->
<mvc:annotation-driven />
<!-- 开启AOP注解扫描 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 处理请求转发 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/config/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 注册拦截器 -->
<mvc:interceptors>
<!-- 登录检查拦截器 -->
<mvc:interceptor>
<!-- 第一个*代表Controller类的访问路径; 第二个*代表Controller方法的访问路径; 即所有的方法都要受此拦截器检查。 -->
<mvc:mapping path="/**" />
<!-- 不受检查的方法 -->
<!-- <mvc:exclude-mapping path="/main/login" />
<mvc:exclude-mapping path="/main/checkLogin" /> -->
<bean class="Project.Util.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<!-- 处理异常 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">error</prop>
</props>
</property>
</bean>
<!-- 声明式事务 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="to*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="java.lang.Exception" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!-- <aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice"
pointcut="within(cn.manytag.ifor7001.controller..*)" />
</aop:config> -->
<!-- 对静态资源文件的访问控制 -->
<mvc:default-servlet-handler />
</beans>
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="MyProject" version="3.0">
<display-name>MyProject</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/config/jsp/index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="movie" type="Project.Dao.MovieDao" />
<typeAlias alias="user" type="Project.Dao.UserDao" />
<typeAlias alias="bus" type="Project.Dao.BusDao" />
</typeAliases>
</configuration>
controller:
package Project.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import Project.Bean.User;
import Project.Service.UserService;
@Controller
@RequestMapping("/userController")
public class UserController {
/**
* 成功
*/
public static final String SUCCESS = "success";
/**
* 失败
*/
public static final String ERROR = "error";
/**
* 账号被禁用
*/
public static final String ACCOUNTDISABLED = "accountdisabled";
/**
* 没有该账号
*/
public static final String NOACCOUNT = "noaccount";
/**
* 账号为空
*/
public static final String NAMENULL = "namenull";
/**
* 密码为空
*/
public static final String PWDNULL = "pwdnull";
@Resource(name = "userService")
private UserService userService;
@RequestMapping("/userlogin")
public String UserLogin(String username,String userpassword, HttpServletRequest request) throws Exception{
request.getSession().setAttribute("sessionName", username);
request.getSession().setAttribute("sessionPwd", username);
String user = userService.UserLogin("sessionName", "sessionPwd");
if(user == "1"){
System.out.println("普通用户--" + username + ",你好!");
return "/userMain.jsp";
}
if(user == "0"){
System.out.println("管理员--" + username + ",你好!");
return "/adminMain.jsp";
}
if(user == ACCOUNTDISABLED){
System.out.println("账号被禁用");
return "/error.jsp";
}
if(user == NOACCOUNT){
System.out.println("没有该账号");
return "/error.jsp";
}
if(user == NAMENULL){
System.out.println("账号为空");
return "/error.jsp";
}
if(user == PWDNULL){
System.out.println("密码为空");
return "/error.jsp";
}
return user;
}
@RequestMapping("/userregedit")
public String UserRegedit(HttpServletRequest request) throws Exception{
String s = userService.UserRegedit((String) request.getSession().getAttribute("username"), (String) request.getSession().getAttribute("userpassword"), (String) request.getSession().getAttribute("userphone"));
System.out.println("");
return s;
}
}
Dao:
package Project.Dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import Project.Bean.User;
import Project.annotation.MybatisDao;
@MybatisDao
public interface UserDao {
/**
* 注册用户
*/
public User regeditUser(User user);
/**
* 通过ID查询用户信息
*
* @param userid
* @return
*/
public User selectByuserId(int userid);
/**
* 通过用户名查询用户信息
*
* @param userid
* @return
*/
public User selectByuserUsername(String username);
/**
* 查询所有用户
*
* @param
* @param
* @return
*/
public List<User> queryAll();
}
Service:
package Project.Service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import Project.Bean.User;
import Project.Dao.UserDao;
@Service
public class UserService {
@Resource
private UserDao userDao;
/**
* 成功
*/
public static final String SUCCESS = "success";
/**
* 失败
*/
public static final String ERROR = "error";
/**
* 账号被禁用
*/
public static final String ACCOUNTDISABLED = "accountdisabled";
/**
* 没有该账号
*/
public static final String NOACCOUNT = "noaccount";
/**
* 账号为空
*/
public static final String NAMENULL = "namenull";
/**
* 密码为空
*/
public static final String PWDNULL = "pwdnull";
public String UserLogin(String username, String userpassword) {
String name = username;
String password = userpassword;
/**
* 判断用户名、密码是否为空或空字符串
*/
if (name == null || name.equals("")) {
System.out.println("用户名为空!");
return NAMENULL;
}
if (password == null || password.equals("")) {
System.out.println("用户密码为空!");
return PWDNULL;
}
/**
* 判断用户 标识符 管理员(0) or 普通用户(1) 禁用(0)正常(1)
*/
User u = userDao.selectByuserUsername(username);
int t = u.getType();// 标识符 管理员(0) or 普通用户(1)
int s = u.getState();// 禁用(0)正常(1)
if(u.getUsername()!= null && u.getUsername() == name){
if (s != 1) {
if (t == 1) {
return "1";
}else{
return "0";
}
} else {
return ACCOUNTDISABLED;
}
}else{
return NOACCOUNT;
}
/**
* 校验用户名密码
*/
}
/**
* 用户注册
*
* @param username
* @param userpassword
* @return
*/
public String UserRegedit(String username, String userpassword,
String userphone) {
String name = username;
String password = userpassword;
String phone = userphone;
/**
* 判断用户名、密码是否为空或空字符串
*/
if (name == null || name.equals("")) {
System.out.println("用户名为空!");
return null;
}
if (password == null || password.equals("")) {
System.out.println("用户密码为空!");
return null;
}
if (phone == null || phone.equals("")) {
System.out.println("手机号为空!");
return null;
}
User o = new User();
o.setUserpassword(name);
o.setUserpassword(password);
o.setUserphone(userphone);
o.setType(1);
o.setState(1);
userDao.regeditUser(o);
return "/regeditUser";
}
}
Mapper:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="Project.Dao.UserDao">
<select id="selectByuserUsername" parameterType="String">
select * from user where
username = #{username}
</select>
<insert id="regeditUser" parameterType="User">
insert into user
(username,userpassword,userphone,type.state)
values
(#{username},#{userpassword},#{userphone},1,1)
ON DUPLICATE KEY
UPDATE
username = #{username},
userpassword = #{userpassword},
userphone = #{userphone},
type = 1,
state = 1
</insert>
</mapper>
还望各位大佬指点江山一下!