Struts2---配置文件讲解及简单登录示例
bean
用于创建一个JavaBean实例
constant
用于Struts2默认行为标签
<!-- 配置web默认编码集,相当于HttpServletRequest.setChartacterEncoding用法,配置了这个相当于配置了一个字符编码过滤器 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 默认Struts2拦截的请求后缀是.action,也就是说我们配置了该元素,会拦截.do的请求,而不是.action,要包含,则在value属性中添加action,do即可 --> <constant name="struts.action.extension" value="do"></constant> <!-- 设置浏览器是否缓存静态内容,默认值为true,在开发阶段建议关闭,防止修改后测试不到 --> <constant name="struts.serve.static.browserCache" value="false"></constant> <!-- 当struts配置文件修改后,系统是否自动重新加载该文件,默认为false。开发阶段建议设置为true,配置好后,更改为false --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 开发模式下使用,这样可以打印除更加详细的错误信息 --> <constant name="struts.devMode" value="true"></constant> <!-- 默认视图主体 --> <constant name="struts.ui.theme" value="simple"></constant>
include
用于引入其它的xml配置文件
package
是包标签,用于区分不同的请求文件的标签,比方说网站前台请求/网站后台请求 -->
<!-- name:包名,用于被别的包调用或继承 extends:继承哪个包,会继承该包下配置信息和拦截器等等 namespace:选填,url连接必须加入namespace属性值。如下:/new/action.xxx --> <package name="test" namespace="/new" extends="struts-default"> <!-- action相当于以前的servlet的概念,对应一个请求,name为请求的url地址;class为处理请求的action类 --> <action name="login" class="com.studyStruts.action.LoginAction"> <!-- 相当于请求处理后要跳转的位置,name为return的值 --> <result name="success">/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package>
简单登录示例
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"> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%=path %> <br /> <%=basePath %> <br /> <form action="<%=path%>/new/login.do" method="post"> 用户名:<input type="text" name="username"> 密码:<input type="password" name="password"> <input type="submit" value="提交"> </form> </body> </html>
LoginAction.java
private String username;
private String password;
public String execute(){
if(username.equals("admin") && password.equals("123")){
return "success";
}else{
return "fail";
}
}
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;
}
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"> <struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.action.extension" value="do"></constant> <constant name="struts.ui.theme" value="simple"></constant> <package name="test" namespace="/new" extends="struts-default"> <action name="login" class="com.studyStruts.action.LoginAction"> <result name="success">/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package> </struts>
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" version="3.1"> <!-- 与项目名称一致 --> <display-name>StudyStruts2</display-name> <!-- 设置url不写文件名时,默认访问的页面 --> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 添加struts过滤器 --> <filter> <filter-name>StrutsFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>StrutsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
http请求时程序处理流程
配置文件连接点