struts2 MVC中的C层,用来接收用户的请求,和返回结果。其他的深入了解,可以请求万能的度娘。
写这其实是我比较懒的表现,有时候,别人问我。心里也明白,但是就是不知道这么说,这或许就是词短,没有语言组织能力吧。也因此痛定思痛,把学过的知识,回顾下,顺便下来了。也好组织下语言,和讲解思路。
开发第一步,导入jar包。
strut2 下载地址 http://struts.apache.org/download.cgi
打开网址如下,如果你不知道导入哪些jar包,可以下载圈中的 min-lib.这里面的jar包是最小依赖jar包,缺了这里面任何一个都不可以的。
2. jar包导入之后,就是在web.xml文件中配置前台控制器,用来接收用户的所有请求,至于过滤器这么配置,如果不会,也不要紧,你可以下载上面 xxx-all.zip 或者 xxx-apps.zip ,解压后,载apps文件夹下有二个war的文件,打开struts2-rest-showcase.war 在WEB-INF下有web.xml
web.xml 配置如下(我自己项目中的,也可以直接拷贝使用)
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 搭建struts2前端控制器,用来过滤用户的请求,提供统一的接口 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.在src文件夹下创建 struts.xml 配置文件
当然如果你不知道这么配置,可以在struts2-rest-showcase.war 在WEB-INF下src里面可以找到struts.xml 复制一份到项目中。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!--
dtd:document type definition,约束文件中的标签规格
配置文件:配置Action类
name:包的名字
package:配置文件的包,将配置文件分包放置管理
namespace:是Actino的访问前缀,即要访问namespace为"/user"的包中的name为"login"的Action:
http://localhost:8080/struts1/user/login
:在namespace的支持下,较彻底的将配置文件分模块管理了。
-->
<struts>
<package name="ab" extends="struts-default" namespace="/user">
<!-- 配置Action
name:等价于Servlet的url-pattern
即要访问该Action 要通过"/user"
class:Action类的全限定名
method:执行方法名
-->
<action name="login" class="com.struts.control.Login" method="userLogin">
<!--
name:视图名
-->
<result name="login">/login.jsp</result>
<result name="index">/index.jsp</result>
</action>
</package>
</struts>
3.到了这里,配置信息就写好了。让我们看看项目中这么实现。
项目结构如图
4.首先写个jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<!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>Insert title here</title>
</head>
<body style="text-align: center;margin-top: 15%;background-color: yellow;">
<form action="<%=basePath %>/user/login" method="post">
用户名:<input type="text" name="userName"/><br/><br/>
密 码:<input type="password" name="passWord"><br/><br/>
日期 :<input type="text" name="birth"/><br/><br/>
<input type="submit" value="登陆">
</form>
</body>
</html>
效果如图
5.页面有了,让我们看看后台如何实现。
package com.struts.control;
import java.util.Date;
public class Login {
// 这里获取前台参数,参数名称和这里名称一致,并在后台设置get set方法
private String userName;
private String passWord;
private Date birth;
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
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;
}
public String userLogin(){
System.out.println("userName:"+userName+"===passWord:"+passWord+"===birth:"+birth);
if(userName.equals("admin")&&passWord.equals("123456")){
return "login";//默认跳转方式为:请求转发 返回字符串是用来选择跳转视图
}else{
return "index";
}
}
}
6.web.xml 和struts.xml在上面已经写过。
关于跳转的关系,如图
7.这里先做个小小测试,后面的会继续更新。
转载于:https://blog.51cto.com/zhuws/1926477