论坛首页 Java企业应用论坛

spring mvc的奇怪问题,关于注解的路径问题

浏览 3790 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-08-05  
web.xml
<servlet>
		<servlet-name>spring3</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring3-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring3</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

springmvc配置文件
<context:component-scan base-package="com.xinxin.autocode.web.controller" />
	<mvc:resources mapping="/resources/**" location="/resources/" />
	<mvc:annotation-driven />

	<!-- DispatcherServlet接收到处理程序返回的视图名称,并解析为视图实现以供显示。 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/pages/" />
		<property name="suffix" value=".jsp" />
	</bean>

index.jsp
<script type="text/javascript">
		window.location.href = "good/index";
	</script>

class类
@Controller
@RequestMapping(value = "/login")
public class LoginController {

	@RequestMapping(value = "/index")
	public String index() {
		return "login";
	}

	@RequestMapping(value = "/login")
	public String login(String username, String password) {
		return "homePage";
	}

}

奇怪的代码是:action="login",按照我的理解应该是action="login/login",
如果是action="login",正确跳转,地址为:http://localhost:8080/autocode/login/login
如果是action="login/login",访问地址就是404,地址为:http://localhost:8080/autocode/login/login/login
<form action="login" method="post">
				<div id="errorMsg"></div>
				<div>
					<label>用户名:</label><input id="username" name="username" />
				</div>
				<div>
					<label>密 码:</label> <input type="password" id="password"
						name="password" />
				</div>
				<div>
					<input type="submit" value="登陆" /> <input type="reset" value="重置" />
				</div>
			</form>

问题总结:
我的理解是,访问路径应该是"类注解的路径/方法注解的路径",但是这么理解有什么不对吗?
采用的措施有,把web.xml的拦截器由/改为*.do还是有问题。
求大神给正确的解答

   发表时间:2013-08-05  
怎么发到这个论坛呢本来是要发到spring的论坛的?????
0 请登录后投票
   发表时间:2013-08-06  
<form action="login" method="post">   典型的相对路径的问题

假设你地址栏地址是 http://a.com/a/b/a.html 则新的地址是
http://a.com/b/login

建议你改成服务器相对地址 即${pageContext.request.contextPath}/login/login
2 请登录后投票
   发表时间:2013-08-06  
试试这两种:
1:action="login",这种需要看当前你的页面所在的路径是什么样的?如果是:
http://localhost:8080/autocode/login/xxxxxxx,则会请求到:http://localhost:8080/autocode/login/login

2:action="/autocode/login/login"

看一下html的form表单:
http://www.w3school.com.cn/tags/att_form_action.asp
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics