struts是什么
Struts是一个开源的web框架。
为什么需要struts
目前许多公司的项目都使用mvc开发模式,不同的公司不同的架构师对mvc的理解有所不同,造成编码规范不统一,不利于项目程序的维护和扩展。strut框架的出现弥补这些缺陷,往往说明了一个新技术的出现必定是来以往存在的缺陷。
struts在项目架构中位置
struts运行原理图
看不清的童鞋可以把图片另存为查看。
快速入门
step
1、新建一个web项目
2、引入struts框架jar包
3、新建表单ActionForm类
//这是一个用户表单,用于填充数据的
public class UserForm extends ActionForm {
//定义属性[定义属性名字的时候,应该和jsp页面的组件名称一致,
//其实只要getUsername和setPassword后半分和组件名一致即可,变量名不一定要一致]
private String username;
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;
}
private String password;
}
4、新建LoginAction类
public class LoginAction extends Action {
//该方法会自动被调用,有点类似servlet中的doGet和doPost
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("xxxxxxxxxxxxxxxxxxxxx");
UserForm userForm=(UserForm)form;
System.out.println("用户名:"+userForm.getUsername());
//return super.execute(mapping, form, request, response);
if(userForm.getPassword().equals("123")) {
return mapping.findForward("ok");
}else {
return mapping.findForward("err");
}
}
}
5、新建login.jsp
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>用户登录</h1>
<form action='/MyTestStruts01/login.do' method='post'>
用户名:<input type='text' name='username' /><br /> 密   码:<input
type='password' name='password' /><br /> <input type='submit'
value='登录' /><br />
</form>
</body>
</html>
6、新建入口index.jsp
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
<script language='javascript'>
function gotoPageNow(){var pageNow= document.getElementById('pageNow').value; window.open('/UsersManager/ManageUsers?pageNow='+pageNow,'_self')};function confirmOper(){return window.confirm('wwww?');};
</script>
</head>
<body>
<jsp:forward page="/WEB-INF/login.jsp"></jsp:forward>
</html>
7、创建struts核心配置文件struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<!-- 配置表单 -->
<struts-config>
<form-beans>
<!-- name是表单名字,可以随意写 -->
<!-- type用于指定表单类的全路径 -->
<form-bean name="userForm" type="com.ydc.form.UserForm"></form-bean>
</form-beans>
<!-- 配置action-->
<action-mappings>
<!-- 配置具体的一个action path:表示将来访问该action的资源名,http://localhost:8080/MyTestStruts/path?-->
<!-- name用于关联某一个表单-->
<!-- type用于指定action类的全路径 -->
<action path="/login" name="userForm" scope="request" type="com.ydc.actions.LoginAction">
<!-- 配置跳转关系-->
<!-- name 表示返回结果名称 path:转发到哪个页面去 -->
<forward name="ok" path="/WEB-INF/welcom.jsp"></forward>
<forward name="err" path="/WEB-INF/err.jsp"></forward>
</action>
</action-mappings>
</struts-config>
看见没,action和form表单以及它们的对应关系都在该文件中配置。
8、创建web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<!-- 配置过滤器 -->
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.ydc.filters.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
当程序启动时,tomcat率先加载web.xml文件,所以把struts框架初始化置于此。
9、创建MyFilter1过滤器
/**
* Servlet implementation class MyFilter1
*/
@WebServlet("/MyFilter1")
public class MyFilter1 extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L;
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("utf-8");//设置接收编码
arg2.doFilter(arg0, arg1);
arg1.setContentType("text/html;charset=UTF-8");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
该类主要处理中文乱码,当程序调起LoginAction的execute方法时,先执行该类的doFilter方法,再执行LoginAction的execute的方法里面的主体代码。过滤器需要在web.xml文件中配置,配置如下:
<!-- 配置过滤器 -->
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>com.ydc.filters.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
10、测试运行
看到没有,数据已经被struts框架填充到UserForm表单中,在这里可以直接从表单中获取。图中“userForm”指的是struts-config.xml文件中表单配置变量名称。