菜鸟求助:springMVC配置问题

u011000069 2017-03-06 09:06:24
整个工程文档结构如下:


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">

<servlet>
<servlet-name>DatabaseController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DatabaseController</servlet-name>
<url-pattern>/studentsmanager/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<!--处理从页面传递中文到后台而出现的中文乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>

</web-app>


DatabaseController-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.tansun.spds" />
<context:annotation-config />
<mvc:annotation-driven />

<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>


DatabaseController.java:

@Controller
@RequestMapping(value = "/studentsmanager")
public class DatabaseController {
@RequestMapping(value = "/insert")
public ModelAndView insertPage(HttpServletRequest request, HttpServletResponse response){
Student student = getAttrFromPage(request);//忽略函数实现
ModelAndView mav = new ModelAndView("consequencePage");
boolean success = false;
try{
service.initConnection();
if(service.insert(student)){
success = true;
}
else;
service.closeConnection();
}
catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
setAttrToPage(mav, new Student());
mav.addObject("consequence", "失败");
return mav;
}
if(success){
setAttrToPage(mav, student);
mav.addObject("consequence", "成功");
return mav;
}
else{
setAttrToPage(mav, new Student());
mav.addObject("consequence", "失败(数据库操作失败)");
return mav;
}


insert.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加学生数据</title>
</head>
<body>
<h3>增加学生数据</h3>
<form action="./studentsmanager/insert" method="post">
<label for="name">姓名</label>
<input name="name" type="text" value="请输入姓名"/>
<br/>
<label for="name">性别</label>
<input name="sex" type="text" value="请输入性别"/>
<br/>
<label for="name">学校</label>
<input name="school" type="text" value="请输入学校"/>
<br/>
<label for="name">年龄</label>
<input name="age" type="text" value="请输入年龄"/>
<br/>
<label for="name">年级</label>
<input name="grade" type="text" value="请输入年级"/>
<br/>
<label for="name">学号</label>
<input name="identifier" type="text" value="请输入学号"/>
<br/>
<label for="name">自我介绍</label>
<input name="selfIntroduction" type="text" value="请输入自我介绍"/>
<br/>
<input value="提交" type="submit"/>
</form>
<a href="index.html">返回主页</a>
</body>
</html>


现在情况是这样的:打开http://localhost:8080/SpringMVCWithMySql/insert.html正常,但是提交表单时在http://localhost:8080/SpringMVCWithMySql/studentsmanager/insert处错误如下:
HTTP Status 404 - /SpringMVCWithMySql/studentsmanager/insert


type Status report

message /SpringMVCWithMySql/studentsmanager/insert

description The requested resource is not available.

控制台没有任何报错。菜鸟一个,还请各位多多批判,脑袋已经犯晕了。
...全文
146 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011000069 2017-03-07
  • 打赏
  • 举报
回复
引用 9 楼 donggua3694857 的回复:
[quote=引用 7 楼 u011000069 的回复:] [quote=引用 6 楼 donggua3694857 的回复:] [quote=引用 5 楼 u011000069 的回复:] [quote=引用 4 楼 donggua3694857 的回复:] 咋看一眼url感觉是studentsmanager/studentsmanager/insert
结果的确如此。只是不解,我一直以为servlet-mapping里的url-pattern的意思是匹配合适的url到控制器,不会额外再添加字符串到url里去,难道pattern里写的匹配规则会被额外添加进访问的url里面?[/quote]你在配一个通配符就好了[/quote] 不配通配符就会把pattern的规则额外添加进去吗?[/quote]我觉得应该是这样的:前面那一段studentsmanager是用来配置你对应的controller的,后面那一段是studentsmanager/insert你配的springmvc的地址映射,两段的作用不一样。。日常来说springmvc配xml配个/就好了[/quote] 多谢了。
GrayHJX 2017-03-07
  • 打赏
  • 举报
回复
引用 7 楼 u011000069 的回复:
[quote=引用 6 楼 donggua3694857 的回复:] [quote=引用 5 楼 u011000069 的回复:] [quote=引用 4 楼 donggua3694857 的回复:] 咋看一眼url感觉是studentsmanager/studentsmanager/insert
结果的确如此。只是不解,我一直以为servlet-mapping里的url-pattern的意思是匹配合适的url到控制器,不会额外再添加字符串到url里去,难道pattern里写的匹配规则会被额外添加进访问的url里面?[/quote]你在配一个通配符就好了[/quote] 不配通配符就会把pattern的规则额外添加进去吗?[/quote]我觉得应该是这样的:前面那一段studentsmanager是用来配置你对应的controller的,后面那一段是studentsmanager/insert你配的springmvc的地址映射,两段的作用不一样。。日常来说springmvc配xml配个/就好了
幽饮烛 2017-03-07
  • 打赏
  • 举报
回复
不同版本的 spring mvc 的配置是不同的。
u011000069 2017-03-07
  • 打赏
  • 举报
回复
引用 6 楼 donggua3694857 的回复:
[quote=引用 5 楼 u011000069 的回复:] [quote=引用 4 楼 donggua3694857 的回复:] 咋看一眼url感觉是studentsmanager/studentsmanager/insert
结果的确如此。只是不解,我一直以为servlet-mapping里的url-pattern的意思是匹配合适的url到控制器,不会额外再添加字符串到url里去,难道pattern里写的匹配规则会被额外添加进访问的url里面?[/quote]你在配一个通配符就好了[/quote] 不配通配符就会把pattern的规则额外添加进去吗?
GrayHJX 2017-03-07
  • 打赏
  • 举报
回复
引用 5 楼 u011000069 的回复:
[quote=引用 4 楼 donggua3694857 的回复:] 咋看一眼url感觉是studentsmanager/studentsmanager/insert
结果的确如此。只是不解,我一直以为servlet-mapping里的url-pattern的意思是匹配合适的url到控制器,不会额外再添加字符串到url里去,难道pattern里写的匹配规则会被额外添加进访问的url里面?[/quote]你在配一个通配符就好了
u011000069 2017-03-07
  • 打赏
  • 举报
回复
引用 4 楼 donggua3694857 的回复:
咋看一眼url感觉是studentsmanager/studentsmanager/insert
结果的确如此。只是不解,我一直以为servlet-mapping里的url-pattern的意思是匹配合适的url到控制器,不会额外再添加字符串到url里去,难道pattern里写的匹配规则会被额外添加进访问的url里面?
GrayHJX 2017-03-07
  • 打赏
  • 举报
回复
咋看一眼url感觉是studentsmanager/studentsmanager/insert
u011000069 2017-03-07
  • 打赏
  • 举报
回复
引用 2 楼 zssazrael 的回复:
<servlet-mapping> <servlet-name>DatabaseController</servlet-name> <url-pattern>/studentsmanager/</url-pattern> </servlet-mapping> 配的有问题哦 把地址换成 http://localhost:8080/SpringMVCWithMySql/studentsmanager/studentsmanager/insert 看看还会不会 404
哎,不会404了。但是我一直以为servlet-mapping里的url-pattern的意思是匹配合适的url到控制器,不会额外再添加字符串到url里去,难道pattern里写的匹配规则会被额外添加进访问的url里面?
幽饮烛 2017-03-07
  • 打赏
  • 举报
回复
<servlet-mapping> <servlet-name>DatabaseController</servlet-name> <url-pattern>/studentsmanager/</url-pattern> </servlet-mapping> 配的有问题哦 把地址换成 http://localhost:8080/SpringMVCWithMySql/studentsmanager/studentsmanager/insert 看看还会不会 404
u011000069 2017-03-07
  • 打赏
  • 举报
回复
求助,困扰好几天了。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧