Struts环境搭建
1. Struts2部署 1.引入jar包 struts2-core.jar 核心jar包 xwork.jar 核心jar包 javasist-GA 处理字节码 2.web.xml <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 核心过滤器名称 StrutsPrepareAndExcuteFlter 3.view视图页面 4.编写action 1.UserAction implement Action 2.UserAction extends ActionSupport 5.struts.xml 6.部署运行po:
public class Userinfo {
private String uname;
private String upwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
Action:
public class TestAction01 implements Action {
private Userinfo user;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Userinfo getUser() {
return user;
}
public void setUser(Userinfo user) {
this.user = user;
}
public String execute() throws Exception {
this.setMessage("你好:" + this.user.getUname());
HttpSession session= ServletActionContext.getRequest().getSession();
return "success";
}
}
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.enable.DynamicMethodInvocation" value="false"/>
<!--修改struts.xml 不用重启容器-->
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"/>
<!--<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>-->
<!-- <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"></exception-mapping>
</global-exception-mappings>-->
<action name="helloWord" class="cn.ljl.action.TestAction01">
<result name="success">first.jsp</result>
</action>
</package>
</struts>
first.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Struts</title>
</head>
<body>
<h2>Struts2 第一个案例</h2>
<s:property value="message"></s:property>
<form action="helloWord" method="post">
用户名:<input name="user.uname"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
index.jsp:
<html>
<body>
<h2>Struts2 第一个案例</h2>
<form action="helloWord" method="post">
用户名:<input name="uname"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>first.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行结果