Struts2 无法访问直接通过URL访问jsp文件
2016-06-15 11:37
741 查看
Struts2 无法访问直接通过URL访问jsp文件
struts2配置文件如下:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="main" extends="struts-default"> <action name="loginPerson" class="com.escape.action.LoginAction"> <result name="success">/jsp/welcome.jsp</result> <result name="login">/jsp/login.jsp</result> </action> </package> </struts>action类如下:
package com.escape.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { 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; //编写主方法 public String execute(){ if("tangbo".equals(username) && "123456".equals(password)){ return SUCCESS; //这里直接当成string类型 } return LOGIN; } }web.xml如下:
<?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>Struts2MVCTest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>*.action</url-pattern> </filter-mapping> </web-app>login.jsp文件如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="loginPerson"> <s:label value="login system"></s:label> <br/> <s:textfield name="username" label="username"></s:textfield> <s:textfield name="password" label="password"></s:textfield> <s:submit value="login"></s:submit> </s:form> </body> </html>web project的文件目录如下:
浏览器访问jsp文件下的login.jsp发现错误如下:
只有通过http://localhost:8080/Struts2MVCTest/loginPerson.action才能正确访问。
查找原因:直接在jsp文件夹下新建一个jsp通过URL可以直接访问。网上搜索原因,部分人说是因为在jsp中加入了struts2标签库。查看了书的源例程之后看到struts配置如下:
要将login设置成全局result。发现还是不可以,最后再找原因原来是web.xml配置URLpattern时没有配置jsp文件夹,添加如下:
<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>*.action</url-pattern> <url-pattern>/jsp/*</url-pattern> </filter-mapping>然后可以直接通过URL:http://localhost:8080/Struts2MVCTest/jsp/login.jsp直接访问。
排除方法:1.排除web.xml配置问题 2.排除struts2配置问题
相关文章推荐
- Struts - 标签库
- struts1.0于struts2.0的比较
- Struts2中关于namespace的学习心得
- Struts2----自定义action的学习心得
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- struts2
- SSH学习笔记---struts2 spring集成时action的class的值
- ssh--问题记录
- 文章标题 使用ajaxFileupload+struts2完成文件的上传以及回显到jsp的链接地址
- struts 学习笔记
- Struts2 的基本配置
- struts2框架架构流程
- struts2.0拦截器(实现未登录的用户不能访问系统的页面)(转)
- Struts2 面试题分析
- Struts2+jQuery+JSON实现异步交互
- 软件开发疑惑解答
- ajax ssh 整合
- 关于struts2框架学习
- struts2 @Action给方法注解
- struts2学习笔记