标签:struts java action web.xml struts.xml
1、首先先在网上下载struts2.3.16的完整版,里面有几个文件夹,分别为:apps——范例代码,docs——api,lib——类库,src——源码
2、在myeclipse中新建一个java web project,名称为sshTest
3、添加所需要的类包,在WebContent/WEB-INF/lib目录下,以下为必需的类包
4、接下来就是编写struts配置文件,struts配置文件放在src目录下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <constant name="struts.objectFactory" value="spring" /--> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="example" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error" /> </global-exception-mappings> <action name="Login" class="example.LoginAction" method="execute"> <result>/example/Welcome.jsp</result> <result name="turntofirst" type="redirectAction">First</result> </action> <action name="First" class="example.FirstAction" method="execute"> <result name="success">/example/first.jsp</result> </action> </package> <!-- include file="example.xml"/--> </struts>5、然后是编写web.xml配置文件来启动struts框架,配置struts2的核心过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" 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"> <display-name>SSH test</display-name> <!-- listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener--> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>6、部署项目到tomcat中,如果后台报错,
原因两个:
1.lib中多导入包的大原因:去掉struts2-spring-plugin-2.1.8包即可,因为没有用到spring。
2.还有的原因是用spring了,却没加监听器,在web.xml里面加上
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
7、项目结构为
进行测试:
1、配置2个action,其中result中type为redirectAction,转向另一个action
<action name="Login" class="example.LoginAction" method="execute"> <result>/example/Welcome.jsp</result> <result name="turntofirst" type="redirectAction">First</result> </action> <action name="First" class="example.FirstAction" method="execute"> <result name="success">/example/first.jsp</result> </action>2、在src下编写action文件,并加入相应的属性、getter/setter、execute方法
LoginAction:
public String execute() throws Exception {
if("kevin".equals(getUsername())){
return SUCCESS;
}else{
return "turntofirst";
}
}
@SkipValidation
public String form() throws Exception {
return INPUT;
}
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute() {
msg = "第一个struts程序";
return "success";
}如果username为kevin则转向welcome.jsp页面,否则转向first.jsp页面
first.jsp页面用el表达式输出msg
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>first struts</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${msg}
</body>
</html>版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:struts java action web.xml struts.xml
原文地址:http://blog.csdn.net/kevinxxw/article/details/46906787