Struts2的简单配置
2015-11-20 19:47
393 查看
首先我们得配置web.xml,话不多说,一切都在代码中
就下来我们写一个名为的Helloworld的action,看代码:
就下来就可以配置Struts.xml文件了
最后我们提供相关的页面代码:
index.jsp的代码:
error.jsp的代码:
好了,就这样,慢慢看。
[code]<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSH_E</display-name> <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.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
就下来我们写一个名为的Helloworld的action,看代码:
[code]package com.struts.action; import com.opensymphony.xwork2.ActionSupport; public class Helloworld extends ActionSupport{ /** * Helloworld必须继承自ActionSupport */ private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { System.out.println("execute action succeed"); return "helloworld"; } }
就下来就可以配置Struts.xml文件了
[code]<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index"></default-action-ref><!--默认显示的页面 --> <action name="index"><result >/error.jsp</result></action> <action name="helloworld" class="com.struts.action.Helloworld" method="execute"> <result name="helloworld">/welcome.jsp</result> </action> </package> <constant name="struts.i18n.encoding" value="utf-8" /><!-- 解决汉字的编码问题 --> </struts>
最后我们提供相关的页面代码:
index.jsp的代码:
[code]<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <h1>My Form</h1> <form action="helloworld.action" method="post"> <P><input type="submit" name="submit" value="submit"> </form> </body> </html>
error.jsp的代码:
[code]<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <h1>My Form</h1> <h1>error</h1> </body> </html>
好了,就这样,慢慢看。
相关文章推荐
- Struts2的获取Session的两种方式
- java投票系统
- Java进阶(十七)ArrayList与LinkedList的区别
- Java进阶(十七)ArrayList与LinkedList的区别
- Spring security用户验证机制浅谈.
- Spring Security 项目
- spring transaction源码分析--事务架构
- Maven在Myeclipse上配置
- 用Ant实现Java项目的自动构建和部署
- myeclipse中自动提示的设置
- “java.lang.NoClassDefFoundError: javax/mail/Address”解决办法
- java 类文件类型
- 判断两个时间段是否重合
- 关于eclipse创建android项目无法创建class的问题
- java反射和注解
- 如何使用struts2搭建web项目
- java 创建文件 写入内容 读取内容
- Java中各种Stream和Reader,write
- Java学习之Hessian通信基础
- android studio 导入 eclipse 并上传到svn