目的:SpringMVC Spring Mybatis项目上手
方法:写个小项目
要严格按照java项目结构开发
推荐书籍:《javaWeb开发实战经典》
SSI由struts2,spring以及ibatis组成
Struts目前主要负责数据传递和控制方面
spring则依靠其强大的依赖注入技术实现了类似bean托管和整合等功能
ibatis作为一种轻量级的OR Mapping框架,提供了半自动化对象关系映射的实现
参考构架:

配置过程
1、在java Resoures/src下新建文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- xml 编码格式 -->
<!-- .dtd 引用智能感知 -->
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- dev 指debug调试模式-->
<constant name="struts.devMode" value="true" />
<!-- struts 缓存大小-->
<constant name="struts.multipart.maxSize" value="15728640" />
<!-- 包 继承json数据格式-->
<package name="TEST" extends="json-default">
<!-- html页面以json数据格式交互数据-->
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
</result-types>
<!-- 动作名称 类名字 调用方法名 -->
<action name="logintest" class="loginAction" method="login">
<result name="success"></result>
</action>
</package>
</struts>
再建一个applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!-- xml 编码格式 -->
<!-- xml 编码格式控件引用 -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<!-- xml 编码格式控件引用 -->
<bean id="loginAction" class="cn.loginAction">
</bean>
</beans>
配置 web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- xml 编码格式 -->
<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">
<!-- filter 拦截器 名字struts2 -->
<filter>
<filter-name>struts2</filter-name>
<!-- filter 拦截器的类 -->
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 加载文件 /WEB-INF/classes/ 是tomcat的文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 监听-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
form 内写了 action
<form action ="logintest.action">
UserName<input type="text"></br>password<input type="password">
<input id="btn" type="submit">
</form>
submit之后 发起url请求
strust 拦截器 拦截所有 action 以.action结尾的所有请求(web.xml内配置filter)
然后转strust 拦截器的类之后
把请求去struts的配置文件 struts.xml 去找name相同的action
然后去applicationContext.xml找 action 的java类的路径
username<input type="text" name="username"></br>
password<input type="password" name="password">
先给页面元素加名字属性
给类加页面上同名字的变量
然后给类加get/set方法
public class loginAction {
public String username;
public String password;
public void login() {
System.out.print("传递进来的用户名是"+username);
System.out.print("传递进来的密码是"+password);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
注意跳转页面的url是
http://localhost:8080/Exam/logintest.action?username=taoning&password=123456
下面实现登陆页面的页面跳转
1.struts.xml文件
配置一个返回的名字以及变量
名字用于判断是否使用跳转 变量用于指向跳转链接
<action name="logintest" class="loginAction" method="login">
<result name="success">${successPath}</result>
</action>
2.增加一个变量 String
增加set/get方法
给String 赋值 为跳转链接
public String successPath;
public String login() {
System.out.println("传递进来的用户名是"+username+" 传递进来的密码是"+password);
setSuccessPath("success.jsp");
return "success";
}
3.页面获取值
欢迎您 ${username}
<result name="success">${successPath}</result>
这种方式可以让页面获取到所有的get/set的方法
最后达到的目的:
HTML---------JAVA
Sturs2.xml---spring.xml
配置页面动作与后台类的互动
配置 struts 关于通配符的使用 以后 所有的Action 都可以套用 类名+方法名加.Action 注意大小不通用 struts的数组是用1开始的 只有struts的数组是从1开始的
<action name="*_*" class="cn.{1}Action" method="{2}">
<result name="success">${successPath}</result>
<result name="error">${errorPath}</result>
<!-- 返回名称 返回类型 有返回类型的 用于跳转 跳转场景用于 如登陆后跳转至登陆前使用的页面 type 是固定有限的 -->
<result name="redirectAction" type="redirectAction">${redirectAction}</result>
<!-- redirect是重定向 可以向其他网站的页面跳转 与本项目无关 -->
<result name="redirect" type="redirect">${redirect}</result>
<!-- 返回类型为json -->
<result name="returnJson" type="json"></result>
<!-- 返回类型为流文件 用于文件处理 -->
<result name="fileStream" type="Stream"></result>
</action>
在登陆页面上 可以做2个按钮 通过js 替换form的action内容 指向不同的类
```
$(document).ready(function() {
$("#btn").click(function() {
$("#form1").attr("action", "register_register.action");
$("#form1").submit();
})
})
```
注意 是form 不是 from!!
请区分大小写
类名_方法名.Action
登陆失败 弹出页面
在body里面增加一个隐藏属性
<input id="hid1" type="hidden" value="${loginerror}"/>
js文件中添加
如果页面属性 hid1 的值是是1 则弹出报错弹层
注意判断条件 要用两个等于号!!
var hid1value = $("#hid1").val();
if (hid1value == "1") {
layer.alert("您的用户名/密码错误", {
icon : 2,
skin : 'layer-ext-moon'
})
}
判断用户登陆是否正确 如果正确则返回登录成功的页面 失败则展示原主页面,且将 loginerror 数值置为1
if (username.equals("AAA") && password.equals("123")) {
setLoginerror("0");
setSuccessPath("success.jsp");
return "success";
} else {
setLoginerror("1");
setSuccessPath("index.jsp");
return "success";
}
关于注入:
<bean id="loginAction" class="cn.loginAction">
<!-- a是cn.ipAddressAction类的实例化对象 ipAddress 是对象的一个属性,属性必须有get/set方法 -->
<property name ="a" ref ="ip"></property>
</bean>
<bean id="ip" class="cn.ipAddressAction">
<property name ="ipAddress" value = "132.229.115.66"></property>
</bean>
如果配置完发现tomcat报错 按行找问题 可能的问题
1、单词拼写错误。
2、没有set和get方法导致无法注入数据。
3、类名写错 按CTRL无法出现下划线。
链接数据库
1、数据库IP
2、数据库用户名
3、数据库密码
配置 applicationContext.xml
1、定义一个接口
网友评论